标签:des style color java os io strong 文件
3 3 0 1 1 0 2 3 1 2 1 0 2 3 1 0 1 1 1 2
2 -1优先队列水过,注意判断路不通#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <vector> #include <queue> #include <set> using namespace std; const int INF=1<<27; const int maxn=1000; int dis[maxn],m,n; typedef struct node { int p,w; node (int a,int b){p=a;w=b;} friend bool operator <(node a,node b) { if(a.w!=b.w) return a.w<b.w; return a.p<b.p; } }; vector <node> eg[maxn]; void Dijkstra(int src) { for(int i=0;i<n;i++) dis[i]=INF; dis[src]=0; priority_queue <node> Q; Q.push(node(src,dis[src])); while(!Q.empty()) { node v=Q.top();Q.pop(); for(int i=0;i<eg[v.p].size();i++) { node t=eg[v.p][i]; if(dis[t.p]>t.w+v.w) { dis[t.p]=t.w+v.w; Q.push(node(t.p,dis[t.p])); } } } } int main() { int u,v,w; while(cin>>n>>m) { for(int i=0;i<n;i++) eg[i].clear(); while(m--) { cin>>u>>v>>w; eg[u].push_back(node(v,w)); eg[v].push_back(node(u,w)); } int src,en; cin>>src>>en; Dijkstra(src); if(dis[en]<INF) cout<<dis[en]<<endl; else cout<<"-1"<<endl; } return 0; }
HDU 1874-畅通工程续(Dijkstra+优先队列),布布扣,bubuko.com
标签:des style color java os io strong 文件
原文地址:http://blog.csdn.net/qq_16255321/article/details/38655309