标签:
1 #include <stdio.h> 2 #include <string.h> 3 #define inf 9999999 4 int p[110][110]; 5 int dis[110]; 6 int visit[110]; 7 int n; 8 void dijkstra() //模板 9 { 10 int i,j,pos,minn; 11 for (i = 1; i <= n; i ++) 12 { 13 dis[i] = p[1][i]; 14 visit[i] = 0; 15 } 16 dis[1] = 0; 17 18 for (i = 1; i <= n; i ++) 19 { 20 minn = inf; 21 for(j = 1; j <= n; j ++) 22 { 23 if (!visit[j] && dis[j] < minn) 24 { 25 minn = dis[j]; 26 pos = j; 27 } 28 } 29 visit[pos] = 1; 30 for (j = 1; j <= n; j ++) 31 { 32 if (!visit[j] && dis[j] > dis[pos]+p[pos][j]) 33 dis[j] = dis[pos]+p[pos][j]; 34 } 35 } 36 } 37 int change(char str[110]) //处理输入数据 38 { 39 int i; 40 int ans = 0; 41 for (i = 0; str[i]; i ++) 42 ans = ans*10+str[i]-‘0‘; 43 return ans; 44 } 45 int main () 46 { 47 int i,j; 48 char str[110]; 49 while (~scanf("%d",&n)) 50 { 51 for (i = 1; i <= n; i ++) 52 for (j = 1; j <= n; j ++) 53 if (i == j) 54 p[i][j] = 0; 55 else 56 p[i][j] = inf; 57 58 for (i = 2; i <= n; i ++) 59 { 60 for (j = 1; j < i; j ++) 61 { 62 scanf("%s",str); //将数据全存为字符型 63 if (str[0] != ‘x‘) 64 p[i][j] = p[j][i] = change(str); 65 } 66 } 67 68 dijkstra(); 69 int maxx = 0; 70 for (i = 2; i <= n; i ++) 71 { 72 if (dis[i] > maxx) 73 maxx = dis[i]; 74 } 75 printf("%d\n",maxx); 76 } 77 return 0; 78 }
标签:
原文地址:http://www.cnblogs.com/yoke/p/5875853.html