标签:
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 22516 Accepted Submission(s):
7816
#include<stdio.h> #include<string.h> #define MAX 1000+100 #define INF 0x3f3f3f #define da(a,b)(a>b?a:b) int s,d; int low[MAX],visit[MAX],map[MAX][MAX]; int lian[MAX],want[MAX]; int maxn; void dijkstra(int start,int city) { int i,j,next,min,l,xiao,k=0; memset(visit,0,sizeof(visit)); for(i=1;i<=city;i++) { low[i]=map[start][i]; } visit[start]=1; for(i=2;i<=city;i++) { min=INF; for(j=1;j<=city;j++) { if(!visit[j]&&min>low[j]) { min=low[j]; next=j; } } visit[next]=1; for(j=1;j<=city;j++) { if(!visit[j]&&low[j]>low[next]+map[next][j]) low[j]=low[next]+map[next][j]; } } } int main() { int road,i,j,a,b,time,mintime; while(scanf("%d%d%d",&road,&s,&d)!=EOF) { for(i=1;i<MAX;i++) { for(j=1;j<MAX;j++) { if(i==j) map[i][j]=0; else map[i][j]=INF; } } maxn=0; while(road--) { scanf("%d%d%d",&a,&b,&time); if(maxn<da(a,b)) maxn=da(a,b); if(map[a][b]>time) map[a][b]=map[b][a]=time; } for(i=1;i<=s;i++) scanf("%d",&lian[i]); for(i=1;i<=d;i++) scanf("%d",&want[i]); mintime=INF; for(i=1;i<=s;i++) { dijkstra(lian[i],maxn); for(j=1;j<=d;j++) { if(mintime>low[want[j]]) mintime=low[want[j]]; } } printf("%d\n",mintime); } return 0; }
不知道为什么上边的代码将判断最小的步骤放入主函数中就可以通过 下边这个将判断最小的步骤放入子函数中就一直runtime error,希望有看见的大神帮助小弟解答谢谢了,下边附上问题代码:
#include<stdio.h> #include<string.h> #define MAX 1000+100 #define INF 0x3f3f3f #define da(c,d)(c>d?c:d) int s,d; int low[MAX],visit[MAX],map[MAX][MAX]; int lian[MAX],want[MAX],lowdis[1010000]; int maxn; void dijkstra() { int i,j,next,min,l,xiao,k=0,mintime=INF; memset(lowdis,0,sizeof(lowdis)); for(l=1;l<=s;l++) { memset(visit,0,sizeof(visit)); for(i=1;i<=maxn;i++) { low[i]=map[lian[l]][i]; } visit[lian[l]]=1; for(i=2;i<=maxn;i++) { min=INF; for(j=1;j<=maxn;j++) { if(!visit[j]&&min>low[j]) { min=low[j]; next=j; } } visit[next]=1; for(j=1;j<=maxn;j++) { if(!visit[j]&&low[j]>low[next]+map[next][j]) low[j]=low[next]+map[next][j]; } } for(j=1;j<=d;j++) { if(mintime>low[want[j]]) mintime=low[want[j]]; } } printf("%d\n",mintime); } int main() { int road,i,j,a,b,time; while(scanf("%d%d%d",&road,&s,&d)!=EOF) { for(i=1;i<MAX;i++) { for(j=1;j<MAX;j++) { if(i==j) map[i][j]=0; else map[i][j]=INF; } } maxn = 0; while(road--) { scanf("%d%d%d",&a,&b,&time); if(maxn<da(a,b)) maxn=da(a,b); if(map[a][b]>time) map[a][b]=map[b][a]=time; } for(i=1;i<=s;i++) scanf("%d",&lian[i]); for(i=1;i<=d;i++) scanf("%d",&want[i]); dijkstra(); } return 0; }
标签:
原文地址:http://www.cnblogs.com/tonghao/p/4518069.html