Agri-Net
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
题意:
农夫约翰已经当选镇长了!他的一个运动承诺之一是将互联网连接到该地区的所有农场。当然,他需要你的帮助。
农夫约翰为他的农场订了高速连接,并将与其他农民分享他的连接。为了最大限度地降低成本,他希望将最小量的光纤连接到所有其他农场。
这个破翻译,叭叭了半天,给我绕晕了,说白了就是最小生成树的求权值和。
任何两个农场之间的距离不会超过100,000。
输入
输入包括几种情况。对于每种情况,第一行包含农场数N(3 <= N <= 100)。以下行包含N x N个连续矩阵,其中每个元素显示从农场到另一个的距离。在逻辑上,它们是N行的N个空格分隔的整数。在物理上它们的长度限制在80个字符之间,所以有些行继续到其他的。当然,对角线将为0,因为距离农场i到自身的距离对于这个问题不是很有意思。
思路:prim直接上模板,稠密图。那数组都给你了,你在写不出来就是过分了。注意数组组后一行那个0,其实是没用的,但人家确实让你输进去了,你也得考虑一下的是不。
AC代码:
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> using namespace std; const int maxn = 105,inf = 10000000; int cost[maxn][maxn]; int mincost[maxn]; bool used[maxn]; int V; int prim() { for (int i = 0; i < V; i ++ ) { mincost[i] = inf; used[i] = false; } mincost[0] = 0; int res = 0; while (1) { int v = -1; for (int u = 0; u < V; u++) { if (!used[u] && (v == -1 || mincost[u] < mincost[v])) v = u; } if (v == -1) break; used[v] = true; res += mincost[v]; for (int u = 0; u < V; u++) { mincost[u] = min(mincost[u], cost[v][u]); } } return res; } int main() { while (cin>>V) { for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { cin>>cost[i][j]; } } cout << prim() << endl; } return 0; }
good luck!今天也是元气满满的一天。