POJ 1258 Agri-Net 克鲁斯卡尔(Kruskal)算法&并查集
2013-04-21 20:38
399 查看
J - Agri-Net
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
POJ 1258
Description
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
Sample Output
解题代码:
View Code
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
POJ 1258
Description
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
解题代码:
View Code
#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> using namespace std; const int maxn = 110; const int maxp = 5000; int rank[maxp], fa[maxp]; struct node { int x, y; int w; bool operator < (const node T) const { return w < T.w; } }town[maxp]; int Find (int x) { if (x != fa[x]) return fa[x] = Find (fa[x]); return x; } void Uni(int x, int y) { if (x == y) return; if (rank[x] > rank[y]) { fa[y] = x; } else { if (rank[x] == rank[y]) rank[y] ++; fa[x] = y; } } int main () { int n, i, j, cun, x, k; int tempa, tempb; long long total; while (~scanf ("%d", &n)) { cun = 0; for (i = 0; i < n; i ++) { for (j = 0; j < n; j ++) { scanf ("%d", &x); if (i < j) town[cun++] = (node){i, j, x}; } } sort (town, town + cun); for (i = 0; i < cun; i ++) { rank[i] = 0; fa[i] = i; } total = 0; for (i = 0; i < cun; i ++) { tempa = Find(town[i].x); tempb = Find(town[i].y); if (tempa != tempb) { total += town[i].w; Uni(tempa, tempb); } } printf ("%lld\n", total); } return 0; }
相关文章推荐
- POJ 1258--Agri-Net【最小生成树 && Kruskal && 水题】
- POJ 1258 Agri-Net (Prim&Kruskal)
- POJ 1258 Agri-Net 最小生成树 prim && kruskal
- Poj 1258 Agri-Net (最小生成树Prim & Kruskal)
- poj1258 - Agri-Net (经典邻接矩阵 求最小生成树) (Prim & Kruskal)
- poj 1258 Agri-Net 【Prim&Kruskal】
- poj 1258 小白算法练习 Agri-Net 最小生成树 prim kruskal
- 最小生成树之Kruskal---POJ1258---Agri-Net
- POJ 1258 Agri-Net (kruskal求最小生成树)
- POJ 1258-Agri-Net (Kruskal)
- poj 1258 Agri-Net 最小生成树 kruskal
- 贪心生成最小生成树-克鲁斯卡尔(Kruskal)算法(归并排序)(并查集)
- poj1258 Agri-Net ——————【 Kruskal 】
- poj 1258 Agri-Net 最小生成树 kruskal
- POJ1258 &&TOJ3197 Agri-Net
- POJ 1258:Agri-Net(最小生成树&&prim)
- POJ 1258 Agri-Net(最小生成树) && POJ 2377 Bad Cowtractors(最大生成树)
- 初级->图算法->最小生成树 poj 1258 Agri-Net
- poj 1258 Agri-Net【Kruskal】
- poj&nbsp;1258&nbsp;Agri-Net(prim)