依然最短路径。。。写得好麻烦,党的才67行。。我的。。开始没理解题意,理解偏了, 看了党的,大悟。。
#include <stdio.h> #include <stdlib.h> #include <limits.h> #include <string.h> int n,r,x,y,weight,count = 1,result; int map[202][202]; void input() { int num = 0,a,b,i; char name[202][32],star[32],des[32]; while(r--) { a=0; b=0; scanf("%s %s %d",&star,&des,&weight); for(i=1; i<=num; i++) { if( strcmp(name[i],star) == 0) a = i; if( strcmp(name[i],des) == 0) b = i; } if(a == 0) { num++; strcpy(name[num],star); a = num; } if(b == 0) { num++; strcpy(name[num],des); b = num; } map[a][b] = map[b][a] = weight; } scanf("%s %s",&star,&des); for(i=1; i<=num; i++) { if( strcmp(name[i],star) == 0) x = i; if( strcmp(name[i],des) == 0) y = i; } } void dijkstra() { int i,k,j,max,now,temp,hash[202],dis[202]; memset(hash,0,(n+1)*sizeof(int)); memset(dis,0,(n+1)*sizeof(int)); now = x; dis[now] = INT_MAX; hash[now] = 1; for(k=1; k<=n; k++) { for(i=1; i<=n; i++) { if( hash[i] ) continue; temp = dis[now] < map[now][i]? dis[now] : map[now][i]; if( dis[i] < temp ) dis[i] = temp; } for(i=1,max = -1; i<=n; i++) if( hash[i] == 0 && dis[i] > max ) { max = dis[i]; now = i; } hash[now] = 1; } result = dis[y]; } void output() { printf("Scenario #%d/n",count++); printf("%d tons/n/n",result); } int main(void) { while( scanf("%d%d",&n,&r)!=EOF && n && r ) { memset(map,0,sizeof(map)); input(); dijkstra(); output(); } return 0; }