您的位置:首页 > 其它

算法总结——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));
}
}
}
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: