九度oj 题目1024:畅通工程

题目描述:
    省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本。现请你编写程序,计算出全省畅通需要的最低成本。
输入:
    测试输入包含若干测试用例。每个测试用例的第1行给出评估的道路条数 N、村庄数目M (N, M < =100 );随后的 N 行对应村庄间道路的成本,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间道路的成本(也是正整数)。为简单起见,村庄从1到M编号。当N为0时,全部输入结束,相应的结果不要输出。
输出:
    对每个测试用例,在1行里输出全省畅通需要的最低成本。若统计数据不足以保证畅通,则输出“?”。
样例输入:
3 3
1 2 1
1 3 2
2 3 4
1 3
2 3 2
0 100
样例输出:
3
?

这道题是九度oj1012的升级版,考察了并查集和最小生成树。此处最小生成树采用了prim算法,使用了数组lowCost来记录与当前包括点集的点的最近距离,通过加入n个点来解决问题。
 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <string>
 5 #define MAX 102
 6 #define inf 0x3f3f3f3f
 7 int next[MAX];
 8 int flag[MAX];
 9 int cost[MAX][MAX];
10 int lowCost[MAX];
11 
12 int find(int x) {
13     while(next[x] != 0) {
14         x = next[x];
15     }
16     return x;
17 }
18 
19 int main(int argc, char const *argv[])
20 {
21     int n,m;
22     scanf("%d",&n);
23     while(n != 0) {
24         int count = 0;
25         scanf("%d",&m);
26         for(int i = 1; i <= m; i++) {
27             next[i] = 0;
28             flag[i] = 0;
29             for(int j = 1; j <= m; j++) {
30                 cost[i][j] = inf;
31             }
32         }
33         for(int i = 0; i < n; i++) {
34             int a,b,c;
35             scanf("%d %d %d",&a,&b,&c);
36             int fa = find(a);
37             int fb = find(b);
38             if(fa != fb) {
39                 next[fa] = fb;
40             }
41             if(c < cost[a][b])
42             cost[a][b]= cost[b][a] = c;
43         }
44         for(int i = 1; i <= m; i++) {
45             //printf("%d	",next[i]);
46             if(next[i]== 0) count++;
47          }
48          //printf("
");
49         if(count - 1 != 0) {
50             printf("?
");
51         }
52         else {
53            
54             int sumCost = 0;
55             for(int i = 1; i <= m; i++) {
56                 lowCost[i] = cost[1][i];
57             }
58             flag[1] = 1;
59             
60             for(int i = 1; i <= m; i++) {
61                 int min = inf;
62                 int v = -1;
63                 for(int i = 1; i <= m; i++) {
64                     if(flag[i] == 0 && lowCost[i] < min) {
65                         min = lowCost[i];
66                         v = i;
67                     }
68                 }
69                 flag[v] = 1;
70                 sumCost = sumCost + lowCost[v];
71 
72                 for(int i = 1; i <= m; i++) {
73                     if(cost[v][i] < lowCost[i]) {
74                         lowCost[i] = cost[v][i];
75                     }
76                 }
77             }
78 
79             printf("%d
",sumCost);
80         }
81         
82         scanf("%d",&n);
83     }
84 
85     return 0;
86 }

第一次提交wrong answer, 之后在41行加了一句话,if(c < cost[a][b]) , 这说明两个城市之间可以不仅有一条路。

原文地址:https://www.cnblogs.com/jasonJie/p/5677014.html