POJ 1258 Agri-Net (最小生成树)

题目链接:POJ 1258

Describe:
Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.
Input:
The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.
Output:
For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.
Sample Input:
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
Sample Output:
28

题目大意:

有n个农场,人们要把每个农场用光纤连起来,问最少用多长光纤,题目输入n和每个农场间的距离。

解题思路:

很明显的最小生成树,相当于模板题,用的两种方法解的题(kruskal和prim)

AC代码(kruskal):

 

 1 // kruskal
 2 // 应该是有点瑕疵,那个g二维数组可以不用的,懒得改了
 3 #include <iostream>
 4 #include <cstdio>
 5 #include <cstring>
 6 #include <queue>
 7 #define rep(i,n) for(int i = 0; i < n; i++)
 8 using namespace std;
 9 struct edge // 用来表示边的结构体 w为权重即距离,st,ed为端点
10 {
11     int w,st,ed;
12     bool operator < (const edge &a) const
13     {
14         return w > a.w;
15     }
16 };
17 int g[100][100],d[100][100],f[100],r[100]; // 前两个为图,和距离,后两个为并查集
18 void init(int n) // 初始化函数
19 {
20     rep(i,n)
21     {
22         f[i] = i;
23         r[i] = 1;
24     }
25     rep(i,n)
26     {
27         rep(j,n)
28         {
29             g[i][j] = 0;
30             d[i][j] = 0;
31         }
32     }
33 }
34 int getfather(int x) // 以下两个函数为并查集函数
35 {
36     return f[x] == x?x:(f[x] = getfather(f[x]));
37 }
38 void mergeset(int x, int y)
39 {
40     x = getfather(x); y = getfather(y);
41     if(x != y)
42     {
43         if(r[x] <= r[y]) f[x] = y;
44         else f[y] = x;
45         if(r[x] == r[y]) r[y]++;
46     }
47 }
48 int main()
49 {
50     int n,ans,t;
51     while(~scanf("%d",&n))
52     {
53         ans = 0; // 最终答案
54         t = 0; // 记录插入的边数
55         init(n);
56         priority_queue<edge> q; // 小顶堆
57         rep(i,n)
58         {
59             rep(j,n)
60             {
61                 scanf("%d",&d[i][j]);
62                 if(d[i][j] != 0) g[i][j] = g[j][i] = 1; // 边读入,边记录是否存在通路,这一步感觉有点多余了
63             }
64         }
65         rep(i,n) // 向堆中插入边
66         {
67             for(int j = i+1; j < n; j++)
68             {
69                 if(g[i][j] == 1) // 同理,感觉这个应该是多余了,这个二维数组应该可以不要的
70                 {
71                     edge tmp;
72                     tmp.st = i;
73                     tmp.ed = j;
74                     tmp.w = d[i][j];
75                     q.push(tmp);
76                 }
77             }
78         }
79         while(t != (n-1)) // kruskal的做法,取边
80         {
81             edge tem = q.top();
82             q.pop();
83             if(getfather(tem.st) == getfather(tem.ed)) continue;
84             mergeset(tem.st,tem.ed);
85             ans += tem.w;
86             t++;
87         }
88         printf("%d\n",ans);
89     }
90     return 0;
91 }

 

 

 

AC代码(prim):

这个经过修改了,那个多余的二维数组已经被去掉了

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <queue>
 5 #define rep(i,n) for(int i = 0; i < n; i++)
 6 using namespace std;
 7 struct edge //
 8 {
 9     int w,st,ed;
10     bool operator < (const edge &a) const
11     {
12         return w > a.w;
13     }
14 };
15 int d[100][100],f[100];
16 int n,t,ans;
17 void init(int n) // 初始化
18 {
19     memset(f,0,sizeof(f));
20     rep(i,n)
21         rep(j,n)
22             d[i][j] = 0;
23 
24 }
25 bool check(int n) // 检查是否每个顶点都用到了
26 {
27     rep(i,n)
28         if(f[i] == 0)
29             return true;
30     return false;
31 }
32 int main()
33 {
34     while(~scanf("%d",&n))
35     {
36         t = 0;
37         ans = 0;
38         priority_queue<edge> q;
39         init(n);
40         rep(i,n)
41             rep(j,n)
42                 scanf("%d",&d[i][j]);
43         while(true) // prim做法,取点
44         {
45             f[t] = 1;
46             if(!check(n)) break;
47             rep(i,n)
48             {
49                 if(f[i] != 1)
50                 {
51                     edge tmp;
52                     tmp.st = t; tmp.ed = i;
53                     tmp.w = d[t][i];
54                     q.push(tmp);
55                 }
56             }
57             edge tem = q.top(); q.pop();
58             while(f[tem.ed]) {tem = q.top(); q.pop();} // 注意这个,没有这个会陷入循环,生成树不能有回路
59             t = tem.ed;
60             ans += tem.w;
61         }
62         printf("%d\n",ans);
63     }
64     return 0;
65 }

 

posted @ 2020-08-19 11:25  不敢说的梦  阅读(256)  评论(0)    收藏  举报