Agri-Net
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 63242 | Accepted: 26155 |
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.
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
#include <stdio.h> #include <algorithm> using namespace std; struct edge{ int from, to, val; bool operator < (const edge& x) const{ return val < x.val; } }e[10111]; int p[111], a[101][101]; int findset(int x){ return p[x] == x ? x : p[x] = findset(p[x]); } int main(){ int n, m, u, v, w, num; while(scanf("%d", &n) != EOF){ for(int i = 1; i <= n; ++i){ for(int j = 1; j <= n; ++j){ scanf("%d", &a[i][j]); } } num = 0; for(int i = 1; i <= n; ++i){ for(int j = i + 1; j <= n; ++j){ e[++num].from = i; e[num].to = j; e[num].val = a[i][j]; } } sort(e + 1, e + 1 + num); int ans = 0; for(int i = 1; i <= n; ++i){ p[i] = i; } for(int i = 1; i <= num; ++i){ u = findset(e[i].from); v = findset(e[i].to); if(u != v){ p[u] = v; ans += e[i].val; } } printf("%d\n", ans); } } /* 题意: 100个点,各处每两个点之间的边权,求最小生成树。 思路: 并查集维护一下集合,对所有边排序后扫一遍即可。 */
构建农业网络
本文介绍了一个典型的最小生成树问题,即如何使用最少的光纤连接多个农场,确保任意两个农场间的数据包都能互相传输。通过并查集算法实现最优解。
520

被折叠的 条评论
为什么被折叠?



