Choose the best route
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14776 Accepted Submission(s): 4801
Each case begins with three integers n, m and s,(n<1000,m<20000,1=<s<=n) n stands for the number of bus stations in this city and m stands for the number of directed ways between bus stations .(Maybe there are several ways between two bus stations .) s stands for the bus station that near Kiki’s friend’s home.
Then follow m lines ,each line contains three integers p , q , t (0<t<=1000). means from station p to station q there is a way and it will costs t minutes .
Then a line with an integer w(0<w<n), means the number of stations Kiki can take at the beginning. Then follows w integers stands for these stations.
5 8 5 1 2 2 1 5 3 1 3 4 2 4 7 2 5 6 2 3 5 3 5 1 4 5 1 2 2 3 4 3 4 1 2 3 1 3 4 2 3 2 1 1
1-1
AC代码:
#include<stdio.h> #include<cstdio> #include<string.h> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define MAX 100000000 int n,m,s,p,q,t,map[1000][1000]; int mark[1000],f[1000]; int dijkstra() { int i,j,min,k; for(i=0;i<=n;i++) { f[i]=map[0][i]; mark[i]=0; } f[0]=0; mark[0]=1; for(i=1;i<=n;i++) { min=MAX; for(j=1;j<=n;j++) { if(!mark[j]&&f[j]<min) { min=f[j]; k=j; } } if(min==MAX) break; mark[k]=1; for(j=1;j<=n;j++) { if(!mark[j]&&f[j]>f[k]+map[k][j]) f[j]=f[k]+map[k][j]; } } if(f[s]>=MAX) return -1; else return f[s]; } int main() { int i,j,ans,w,st; while(scanf("%d%d%d",&n,&m,&s)!=EOF) { for(i=0;i<=n;i++) for(j=0;j<=n;j++) { if(i==j) map[i][j]=0; else map[i][j]=MAX; } for(i=0;i<m;i++) { scanf("%d%d%d",&p,&q,&t); if(map[p][q]>t) map[p][q]=t; } scanf("%d",&w); while(w--) { scanf("%d",&st); map[0][st]=0; } ans=dijkstra(); printf("%d\n",ans); } return 0; }