标签:
1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #include<vector> 5 #include<algorithm> 6 using namespace std; 7 8 struct edge 9 { 10 int u,v,w; 11 edge(int u,int v,int w):u(u),v(v),w(w){}; 12 }; 13 14 struct heapnode 15 { 16 int u,d; 17 bool operator < (const heapnode& temp)const 18 { 19 return d>temp.d; 20 } 21 }; 22 23 vector<int>G[1005]; 24 vector<edge>edges; 25 const int inf=0x3f3f3f3f; 26 int vis[1005]; 27 int d[1005]; 28 int ed; 29 int m,n; 30 31 void init() 32 { 33 for(int i=1;i<=m;i++) 34 G[i].clear(); 35 edges.clear(); 36 } 37 38 void addedge(int u,int v,int w) 39 { 40 edges.push_back(edge(u,v,w)); 41 int m=edges.size(); 42 G[u].push_back(m-1); 43 } 44 45 void dijkstra(int ed) 46 { 47 memset(vis,0,sizeof(vis)); 48 priority_queue<heapnode>q; 49 q.push((heapnode){ed,0}); 50 for(int i=1;i<=m;i++) 51 d[i]=inf; 52 d[ed]=0; 53 while(!q.empty()) 54 { 55 int u=q.top().u; 56 q.pop(); 57 if(vis[u]) 58 continue; 59 vis[u]=1; 60 for(int i=0;i<G[u].size();i++) 61 { 62 edge& e=edges[G[u][i]]; 63 if(d[e.v]>d[u]+e.w) 64 { 65 d[e.v]=d[u]+e.w; 66 q.push((heapnode){e.v,d[e.v]}); 67 } 68 } 69 } 70 } 71 72 int main() 73 { 74 int st,a,b,c,num; 75 while(scanf("%d%d%d",&m,&n,&ed)!=EOF) 76 { 77 init(); 78 for(int i=0;i<n;i++) 79 { 80 scanf("%d%d%d",&a,&b,&c); 81 addedge(b,a,c); 82 } 83 dijkstra(ed); 84 scanf("%d",&num); 85 int ans=inf; 86 while(num--) 87 { 88 scanf("%d",&st); 89 ans=min(ans,d[st]); 90 } 91 if(ans==inf) 92 printf("-1\n"); 93 else 94 printf("%d\n",ans); 95 } 96 return 0; 97 }
HDU 2680 Choose the best route(dijkstra+优先队列优化)
标签:
原文地址:http://www.cnblogs.com/homura/p/4725590.html