ZOJ 1952( Dijkstra )要求卡车的最大载货量,即是求dist[]的最小值这里关键是把字符串转化为数字存储在邻接矩阵cost[][]中.开始看了党姐的代码不懂,又看了一遍,大悟!

 1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4 #define MAX 90000000
5
6 int count = 0;
7 char name[201][40];
8 int find(char *a) //此函数实现将字符串转化为数字,剽窃党姐的呵呵>_<
9 {
10 int i;
11 for( i = 1; i <= count; i++ )
12 if( strcmp(a,name[i]) == 0 )
13 return i;
14 count = i;
15 strcpy(name[i],a);
16 return i;
17 }
18
19 int main()
20 {
21 int n, r, l, s[201], dist[201], cost[201][201];
22 char a[40], b[40];
23 int i, j, start, end, now, k=1;
24 int temp, max, from, to;
25
26 while(scanf("%d%d",&n,&r) != EOF)
27 {
28 if (n==0 && r==0) break;
29 for(i=1; i<=n; i++)
30 {
31 dist[i] = 0;
32 s[i] = 0;
33 for(j=1; j<=n; j++)
34 {
35 cost[i][j] = 0;
36 }
37 }
38 for(i=1; i<=r; i++)
39 {
40 scanf("%s%s",a,b);
41 from = find(a);
42 to = find(b);
43 scanf("%d",&l);
44 cost[from][to] = cost[to][from] = l;
45 }
46 scanf("%s%s",a,b);
47 start = find(a);
48 end = find(b);
49
50 now = start;
51 dist[now] = MAX;
52 s[now] = 1;
53 for(i=1; i<n; i++)
54 {
55 max = -1;
56 for(j=1; j<=n; j++)
57 {
58 if (s[j] == 0)
59 temp = dist[now]<cost[now][j] ? dist[now]:cost[now][j];
60 if(dist[j] < temp)
61 dist[j] = temp;
62 }
63 for(j=1; j<=n; j++)
64 if(s[j]==0 && dist[j] > max)
65 {
66 now = j;
67 max = dist[now];
68 }
69 s[now] = 1;
70 }
71 printf("Scenario #%d\n",k++);
72 printf("%d tons\n\n",dist[end]);
73 }
74 return 0;
75 }
76
posted @ 2011-10-19 16:53  zhongya  阅读(302)  评论(0编辑  收藏  举报