Agri-Net
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 38 Accepted Submission(s) : 8
Problem 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
Source
PKU
思路:
prim算法,前面已经介绍过了!
代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int g[305][305];
int n,i,j;
int minn[305];
bool used[305];
int sum;
int main()
{
ios::sync_with_stdio(false);
int n;
while(cin>>n)
{
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
cin>>g[i][j];
memset(minn,0x7f,sizeof(minn));minn[1]=0;
memset(used,1,sizeof(used));
for(i=1;i<=n;i++)
{
int k=0;
for(j=1;j<=n;j++)
if(used[j]&&minn[k]>minn[j])k=j;
used[k]=0;
for(j=1;j<=n;j++)
if(used[j]&&minn[j]>g[k][j])
minn[j]=g[k][j];
}
int sum=0;
for(j=1;j<=n;j++)sum+=minn[j];
cout<<sum<<endl;
}
}
不知道为毛用kruskal算法就是不能AC、此题目数组开到9901出现run timerrror情况,捯饬好久都没有通过!现附上代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
struct point
{
int x;
int y;
int v;
};
point a[9901];
int fat[101];
int n,i,j,x,m,tot,k;
int father(int x)
{
if (fat[x] != x) fat[x] = father(fat[x]);
return fat[x];
}
void unionn(int x,int y)
{
int fa = father(x);
int fb = father(y);
if (fa != fb) fat[fa] = fb;
}
int cmp(const point &a,const point &b)
{
if (a.v < b.v) return 1;
else return 0;
}
int main()
{
while(cin >> n)
{
tot=0,k=0;
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
{
cin >> x;
if (x != 0)
{
m++;
a[m].x = i; a[m].y = j; a[m].v = x;
}
}
for (i = 1; i <= n; i++) fat[i] = i;
sort(a+1,a+m+1,cmp);
for (i = 1; i <= m; i++)
{
if (father(a[i].x) != father(a[i].y))
{
unionn(a[i].x,a[i].y);
tot += a[i].v;
k++;
}
if (k == n-1) break;
}
cout << tot<<endl;
}
return 0;
}