Change of Scenery
Time Limit: 10000ms
Memory Limit: 262144KB
This problem will be judged on CodeForcesGym. Original ID: 100753E64-bit integer IO format: %I64d Java class name: (Any)
解题:最短路
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef pair<int,int> pii; 4 const int maxn = 10010; 5 struct arc{ 6 int to,cost,next; 7 arc(int x = 0,int y = 0,int z = -1){ 8 to = x; 9 cost = y; 10 next = z; 11 } 12 }e[2010000]; 13 int head[maxn],cnt[maxn],d[maxn],tot,N,M,K; 14 void add(int u,int v,int w){ 15 e[tot] = arc(v,w,head[u]); 16 head[u] = tot++; 17 } 18 priority_queue<pii,vector<pii>,greater<pii>>q; 19 bool done[maxn]; 20 void dijkstra(){ 21 while(!q.empty()) q.pop(); 22 memset(d,0x3f,sizeof d); 23 memset(cnt,0,sizeof cnt); 24 q.push(pii(d[1] = 0,cnt[1] = 1)); 25 while(!q.empty()){ 26 int u = q.top().second; 27 q.pop(); 28 if(done[u]) continue; 29 done[u] = true; 30 for(int i = head[u]; ~i; i = e[i].next){ 31 if(d[e[i].to] > d[u] + e[i].cost){ 32 d[e[i].to] = d[u] + e[i].cost; 33 cnt[e[i].to] = cnt[u]; 34 q.push(pii(d[e[i].to],e[i].to)); 35 }else if(d[e[i].to] == d[u] + e[i].cost) 36 cnt[e[i].to]++; 37 } 38 } 39 } 40 int main(){ 41 int x,u,v,w; 42 while(~scanf("%d%d%d",&N,&M,&K)){ 43 memset(head,-1,sizeof head); 44 while(K--) scanf("%d",&x); 45 for(int i = tot = 0; i < M; ++i){ 46 scanf("%d%d%d",&u,&v,&w); 47 add(u,v,w); 48 add(v,u,w); 49 } 50 dijkstra(); 51 puts(cnt[N] > 1?"yes":"no"); 52 } 53 return 0; 54 } 55 /* 56 3 3 3 57 1 2 3 58 1 2 1 59 2 3 2 60 1 3 3 61 4 5 2 62 1 4 63 1 2 2 64 2 4 1 65 1 3 1 66 3 4 2 67 1 4 2 68 */