标签:des style blog java color strong
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13590 Accepted Submission(s): 3181
还是一道dijkstra最短路的题目,还是依旧的那么恶心的卡你。1.必须先将起点和终点保存下来,否则下面如果没有出现起点和终点的话你就会出;2.起点和终点相同的话就等于0;3.当有2点相同的路的时候,取最短。
#include <stdio.h> #include <string.h> #define maxn 0x3f3f3f3 char str[155][35]; int map[155][155], dis[155], visited[155]; void Dijkstra(int start, int n) { int mind, pre; for(int i = 0; i<n; i++) { dis[i] = map[start][i]; visited[i] = 0; } visited[start] = 1; for(int i = 0; i<n; i++) { mind = maxn; for(int j = 0; j<n; j++) { if(mind > dis[j] && !visited[j]) { mind = dis[j]; pre = j; } } visited[pre] = 1; for(int j = 0; j<n; j++) { if(dis[j] > dis[pre]+map[pre][j] && !visited[j]) dis[j] = dis[pre]+map[pre][j]; } } } int Find(char * stra, int & m) { int i; for(i = 0; i<m; i++) { if(strcmp(str[i], stra) == 0) break ; } if(i == m) { strcpy(str[i], stra); m++; } return i; } int main() { int n, m, x; char start[35], finish[35]; char stra[35], strb[35]; while(scanf("%d", &n)!=EOF && n!=-1) { m = 0; scanf("%s%s", start, finish); Find(start, m); Find(finish, m); for(int i = 0; i<155; i++) { for(int j = 0; j<155; j++) { if(i == j) map[i][j] = 0; else map[i][j] = maxn; } } while(n--) { scanf("%s%s%d", stra, strb, &x); if(map[Find(stra, m)][Find(strb, m)] > x || map[Find(strb, m)][Find(stra, m)] >x) { map[Find(stra, m)][Find(strb, m)] = x; map[Find(strb, m)][Find(stra, m)] = x; } } Dijkstra(Find(start, m), m); if(visited[Find(finish, m)]) printf("%d\n", dis[Find(finish, m)]); else printf("-1\n"); } return 0; }
hdu 2112 HDU Today,布布扣,bubuko.com
标签:des style blog java color strong
原文地址:http://www.cnblogs.com/fengxmx/p/3837882.html