Agri-Net
| Time Limit: 1000MS | Memory Limit: 10000K |
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————————————————————集训9.7的分割线————————————————————
思路:Prim水题。不解释。一天8题的祭品。为了刷蓝皮书。
代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <map>
#include <iostream>
#define INF 2e9
using namespace std;
/****************************************/
const int N = 105;
int n, m;
int mat[N][N], lowcost[N];
void prim()
{
int sum = 0;
for(int i = 0; i < n; i++)
lowcost[i] = mat[0][i];
lowcost[0] = -1;
int k;
for(int i = 1; i < n; i++) {
int mini = INF;
for(int j = 0; j < n; j++) if(lowcost[j] != -1) {
if(lowcost[j] < mini) {
mini = lowcost[j];
k = j;
}
}
sum += mini;
lowcost[k] = -1;
for(int j = 0; j < n; j++) if(lowcost[j] != -1) {
lowcost[j] = min(lowcost[j], mat[k][j]);
}
}
printf("%d\n", sum);
}
int main()
{
#ifdef J_Sure
freopen("1258.in", "r", stdin);
// freopen(".out", "w", stdout);
#endif
while(scanf("%d", &n)!=EOF) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
scanf("%d", &mat[i][j]);
if(i != j) {//这里写成mat[i][j]!=0 WA了几次
mat[j][i] = mat[i][j];
}
}
}
prim();
}
return 0;
}

本文讨论了农民约翰如何使用Prim算法最小化连接所有农场所需的光纤长度,以实现互联网普及。通过给出实例输入和详细说明算法过程,旨在解决实际问题并降低成本。
439

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



