算法总结——spfa(使用优先队列的Bellman_Ford算法)
2015-07-19 15:59
369 查看
#include<cstdio> #include<cstring> #include<algorithm> #include<functional>//greater 使得从小到大 ,less反 //priority_queue<int, vector<int>, less<int> > que 相当于 priority_queue<int> que #include<queue> using namespace std; const int MAX = 100; const int inf = 0x3f3f3f3f; struct edge{ int to, cost; } typedef pair<int, int> P; int V; vector<edge> G[MAX]; int d[MAX];//距s的距离 void dijkstra(int s) { priority_queue<P, vector<P>, greater<P> > que; fill(d, d + V, inf); d[s] = 0; que.push(P(0, s)); while(!que.empty()){ P p = que.top(); que.pop(); int v = p.second; if(d[v] < p.first) continue;//小优化 for(int i = 0; i < G[v].size(); i++){ edge e = G[v][i]; if(d[e.to] > d[v] + e.cost){ d[e.to] = d[v] + e.cost; que.push(P(d[e.to], e.to)); } } } }
相关文章推荐
- Struts2乱码问题的解决办法
- Ubuntu输入密码登陆后又跳回到登录界面
- JavaScript--DOM修改元素的属性
- 黑马程序员——Java反射&动态代理
- 龙贝格算法
- 关于ListView中ViewHolder修饰符的问题
- Ubuntu 14.10 下Hadoop HttpFS 配置
- 菜鸟学习OpenCV(Python)碰到的一些小问题
- stack smashing detected报错
- LeetCode#189 Rotate Array
- [LeetCode] Find Minimum in Rotated Sorted Array
- Tomcat监控xml ,当修改xml后,不用重启tomcat
- 多态的覆盖(重写)
- java中的复制文件
- 收集的网址
- CNN卷积神经网络
- python题目2
- LINUX中system()函数详解
- JAVA的数据类型
- 在VMware下建立一个linux和Windows共享的文件夹