其实这道题以前做过的..不过最近新学了Kruskal,就拿这题练练手:)

Agri-Net
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 48295 Accepted: 20017

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

Source

基本不用怎么解释了,很水很水的一道题,不用多费时间了。
Kruskal实现,数组开小了,贡献N次RE!不知道有多组数据(即使我做过...),贡献1WA:

Source Code

Problem:1258 User:aclolicon
Memory: 920K Time: 110MS
Language: G++ Result:Accepted
    • Source Code

#include<cstdio>
#include<algorithm>
#include<iostream>
struct e{int x, y, v;
};
int n, cnt;
bool used[1000][1000];
int f[1000]={0};
using namespace std;
int find(int x){
if (x != f[x]) f[x] = find(f[x]);
return f[x];
}
bool cmp(e x,e y){
return x.v < y.v;
}

int main(){
while(cin>>n){
e edge[10050];
int num;
for (int i = 0; i < n; i++){
f[i]=i;
for (int j = 0; j < n; j++){
used[i][j]=0;
}
}
cnt=-1;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++){
cin >> num;
if (num != 0 && !used[i][j]) {
cnt++;
edge[cnt].x = i;
edge[cnt].y = j;
edge[cnt].v = num;
}
used[i][j] = used[j][i] = 1;
}
sort(edge, edge+cnt+1, cmp);
// for (int i = 0; i < cnt; i++) cout << edge[i].v << endl;
int res = 0;
for(int i = 0; i <=cnt; i++){
int a = find(edge[i].x), b = find(edge[i].y);
if (a != b) f[a] = b,res += edge[i].v;

// cout << res << endl;
}
cout << res << endl;
}
return 0;
}

 

Prim实现,也是因为多组数据贡献1WA..:

Source Code

Problem: 1258 User: aclolicon
Memory: 360K Time: 0MS
Language: C++ Result: Accepted
    • Source Code

#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
#define INF 0x3f3f3f3f
int g[105][105];
int main(){
int n;
int mincost[10005];
int used[10005];
while(cin>>n){
for (int i=0;i<n;i++)
{
mincost[i]=INF,used[i]=0;
for (int j=0;j<n;j++)
scanf("%d",&g[i][j]);
}
mincost[0]=0;
int res=0;
while(1) {
int v=-1;
for (int i=0;i<n;i++)
if (!used[i]&&(v==-1||mincost[i]<mincost[v])) v=i;
if (v==-1) break;
// printf("%dn",v);
used[v]=1;
res+=mincost[v];
for (int i=0;i<n;i++)
if(!used[i]) mincost[i]=min(g[v][i],mincost[i]);
}
printf("%dn",res);
}

return 0;
}

嗯..相比之下我觉得Prim比上Kruskal真是好一大截了,无论是代码长度还是运行时间等等..不过可能也跟个人水平有关..

那么,下一题就是我要挑战的题目,POJ3522 Slim Span了..GL..