Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 24724 Accepted Submission(s): 8567
6 2 3 1 3 5 1 4 7 2 8 12 3 8 4 4 9 12 9 10 2 1 2 8 9 10
9
<span style="font-size:12px;">#include<cstdio> #include<cstring> #define max 1010 #define INF 0x3f3f3f int map[max][max],dis[max]; int start[max],end[max]; int big(int a,int b) { return a>b?a:b; } void dijkstra(int start,int end_max) { int visit[max]; int i,j,min,next=start; memset(visit,0,sizeof(visit)); for(i=1;i<=end_max;++i) dis[i]=map[start][i]; visit[start]=1; for(i=2;i<=end_max;++i) { min=INF; for(j=1;j<=end_max;++j) { if(!visit[j]&&min>dis[j]) { min=dis[j]; next=j; } } visit[next]=1; for(j=1;j<=end_max;++j) { if(!visit[j]&&dis[j]>dis[next]+map[next][j]) dis[j]=dis[next]+map[next][j]; } } } int main() { int n,s,m,i,j,time,a,b,end_max,mintime; while(scanf("%d%d%d",&n,&s,&m)!=EOF) { for(i=1;i<max;++i)//n表示的是道路数,不是代表城市个数,这里初始化数组用max为上限 { for(j=1;j<max;++j) { if(i==j) map[i][j]=0; else map[i][j]=INF; } } end_max=0;//记录每组测试数据里面最大的城市编号 while(n--) { scanf("%d%d%d",&a,&b,&time); if(end_max<big(a,b)) end_max=big(a,b); if(map[a][b]>time) map[a][b]=map[b][a]=time; } mintime=INF;//记录最短路 for(i=0;i<s;++i) scanf("%d",&start[i]); for(i=0;i<m;++i) scanf("%d",&end[i]); for(i=0;i<s;++i) { dijkstra(start[i],end_max); for(j=0;j<m;++j) { if(dis[end[j]]<mintime) mintime=dis[end[j]]; } } printf("%d\n",mintime); } return 0; }</span>