Problem : 2680 ( Choose the best route ) Judge Status : Accepted RunId : 1154926 Language : G++ Author : zhouxc Code Render Status : Rendered By HDOJ G++ Code Rander Version 0.01 Beta #include "stdio.h" #include "string.h" #define M 100000000 typedef struct{ int Dist; bool Known; }TableEntery; TableEntery Table[1002]; int n,m,s,w,station; int a,b,c,flag,result,map[1002][1002]; bool started[1002]; void InitTable() { for(int i=1;i<=n;i++) { Table[i].Dist=M; Table[i].Known=false; } } void Dijkstra() { InitTable(); flag=0; Table[s].Dist=0; bool succes=true; if(started[s]==true) { result=0; succes=false; } while(succes) { int record,Min=M; for(int i=1;i<=n;i++) { if(!Table[i].Known&&Min>Table[i].Dist) { Min=Table[i].Dist; record=i; //printf("*%d/n",Table[i].Dist); } } // printf("%d %d/n/n",record,Min); if(Min==M) break; Table[record].Known=true; if(started[record]==true) { // printf("%d/n/n",Table[record].Dist); flag=1; result=Table[record].Dist; succes=false; break; } for(int i=1;i<=n;i++) { if(!Table[i].Known&&map[record][i]&&Table[i].Dist>map[record][i]+Table[record].Dist) Table[i].Dist=map[record][i]+Table[record].Dist; //printf("%d %d/n",i,Table[i].Dist); } } } int main() { while(scanf("%d%d%d",&n,&m,&s)!=EOF) { memset(started,false,sizeof(started)); memset(map,0,sizeof(map)); for(int i=1;i<=m;i++) { scanf("%d%d%d",&a,&b,&c); if(map[b][a] == 0||map[b][a]>c) map[b][a]=c; } scanf("%d",&w); for(int i=1;i<=w;i++) { scanf("%d",&station); started[station]=true; } Dijkstra(); if(flag) printf("%d/n",result); else printf("-1/n"); } return 0; } #include "stdio.h" #include "string.h" #define M 100000000 typedef struct{ int Dist; bool Known; }TableEntery; TableEntery Table[1002]; int n,m,s,w,station; int a,b,c,flag,result,map[1002][1002]; bool started[1002]; void InitTable() { for(int i=1;i<=n;i++) { Table[i].Dist=M; Table[i].Known=false; } } void Dijkstra() { InitTable(); flag=0; Table[s].Dist=0; bool succes=true; if(started[s]==true) { result=0; succes=false; } while(succes) { int record,Min=M; for(int i=1;i<=n;i++) { if(!Table[i].Known&&Min>Table[i].Dist) { Min=Table[i].Dist; record=i; //printf("*%d/n",Table[i].Dist); } } // printf("%d %d/n/n",record,Min); if(Min==M) break; Table[record].Known=true; if(started[record]==true) { // printf("%d/n/n",Table[record].Dist); flag=1; result=Table[record].Dist; succes=false; break; } for(int i=1;i<=n;i++) { if(!Table[i].Known&&map[record][i]&&Table[i].Dist>map[record][i]+Table[record].Dist) Table[i].Dist=map[record][i]+Table[record].Dist; //printf("%d %d/n",i,Table[i].Dist); } } } int main() { while(scanf("%d%d%d",&n,&m,&s)!=EOF) { memset(started,false,sizeof(started)); memset(map,0,sizeof(map)); for(int i=1;i<=m;i++) { scanf("%d%d%d",&a,&b,&c); if(map[b][a] == 0||map[b][a]>c) map[b][a]=c; } scanf("%d",&w); for(int i=1;i<=w;i++) { scanf("%d",&station); started[station]=true; } Dijkstra(); if(flag) printf("%d/n",result); else printf("-1/n"); } return 0; }