标签:
1 typedef pair<int,int> pii; 2 struct Edge { 3 int v, w; 4 int next; 5 }edge[MAXM]; 6 int head[MAXN], d[MAXN], tot; 7 bool vis[MAXN]; 8 void addedge(int u, int v, int w) { 9 edge[tot].v = v; 10 edge[tot].w = w; 11 edge[tot].next = head[u]; 12 head[u] = tot++; 13 } 14 void dijkstra(int s) { 15 priority_queue<pii, vector<pii>, greater<pii> > Q; 16 memset(d, 0x3f, sizeof(d)); 17 d[s] = 0; 18 memset(vis, false, sizeof(vis)); 19 Q.push(make_pair(d[s], s)); 20 while(!Q.empty()) { 21 pii tmp = Q.top(); 22 Q.pop(); 23 int x = tmp.second; 24 if(vis[x]) continue; 25 vis[x] = true; 26 for(int i = head[x]; i+1; i = edge[i].next) { 27 if(d[edge[i].v] > d[x] + edge[i].w) { 28 d[edge[i].v] = d[x] + edge[i].w; 29 Q.push(make_pair(d[edge[i].v], edge[i].v)); 30 } 31 } 32 } 33 }
标签:
原文地址:http://www.cnblogs.com/mitrenick/p/4415682.html