标签:dijkstra
#include <cstdio> #include <iostream> #include <queue> using namespace std; const int INF = 1000000; const int maxn = 5005; struct edge{ int y,w; edge(int cy,int ww){ y = cy; w = ww; } }; vector<edge> vec[maxn]; int n,m; struct num{ int d,n; bool operator < (const num& a)const{ return d > a.d; } num(int x,int y){ d = x; n = y; } }; int d1[maxn]; int d2[maxn]; void fun(){ priority_queue<num,vector<num>,less<num> > que; for(int i = 1;i <= n;i++){ d1[i] = INF; d2[i] = INF; } d1[1] = 0; que.push(num(0,1)); while(!que.empty()){ num q = que.top(); que.pop(); int u = q.n; int d = q.d; if(d > d2[u]) continue; int cc = vec[u].size(); for(int i = 0 ; i < cc;i++){ edge e = vec[u][i]; int dd2 = d + e.w; if(d1[e.y] > dd2){ swap(d1[e.y],dd2); que.push(num(d1[e.y],e.y)); } if(d2[e.y] > dd2 && dd2 > d1[e.y]){ d2[e.y] = dd2; que.push(num(d2[e.y],e.y)); } } } } int main() { while(scanf("%d%d",&n,&m)!=EOF){ for(int i = 0;i < m;i++){ int x,y,w; scanf("%d%d%d",&x,&y,&w); vec[x].push_back(edge(y,w)); vec[y].push_back(edge(x,w)); } } fun(); printf("%d\n",d2[n]); return 0;
POJ 3255 Roadblocks Dijkstra 算法变形
标签:dijkstra
原文地址:http://blog.csdn.net/qq_24667639/article/details/45648675