标签:
1 #include<stdio.h> 2 #include<string.h> 3 #define MIN(x,y)(x<y?x:y) 4 const int MAXN=1010; 5 const int INF=0x3f3f3f3f; 6 int n,vis[MAXN],d[MAXN]; 7 int map[MAXN][MAXN]; 8 void initial(){ 9 memset(map,0x3f,sizeof(map)); 10 memset(vis,0,sizeof(vis)); 11 memset(d,INF,sizeof(d)); 12 } 13 void dijskra(int s){ 14 d[s]=0; 15 while(true){ 16 int k=-1; 17 for(int i=1;i<=n;i++) 18 if(!vis[i]&&(k==-1||d[i]<d[k]))k=i; 19 if(k==-1)break; 20 vis[k]=1; 21 for(int i=1;i<=n;i++) 22 d[i]=MIN(d[i],d[k]+map[k][i]); 23 } 24 } 25 int main(){ 26 int m,s,p,q,t,a,e,ans; 27 while(~scanf("%d%d%d",&n,&m,&s)){ 28 initial(); 29 while(m--){ 30 scanf("%d%d%d",&p,&q,&t); 31 if(map[q][p]>t)map[q][p]=t; 32 } 33 scanf("%d",&a); 34 ans=INF; 35 dijskra(s); 36 while(a--){ 37 scanf("%d",&e); 38 ans=MIN(ans,d[e]); 39 } 40 if(ans==INF)puts("-1"); 41 else printf("%d\n",ans); 42 } 43 return 0; 44 }
标签:
原文地址:http://www.cnblogs.com/handsomecui/p/4737553.html