标签:
#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)); } } } }
标签:
原文地址:http://www.cnblogs.com/zero-begin/p/4658752.html