标签:
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 45145 | Accepted: 18534 |
Description
Input
Output
Sample Input
4 0 4 9 21 4 0 8 17 9 8 0 16 21 17 16 0
Sample Output
28
看到给的数据应该是 用prime 做比较方便 ,但闲的没事就用Kruskal写了一遍。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
struct node{
int u, v, w;
};
node map[11000];
int per[110], n;
int cmp(node a, node b){
return a.w < b.w;
}
void init(){
for(int i = 1; i <= n ;++i)
per[i] = i;
}
int find(int x){
if(x == per[x])
return x;
return per[x] = find(per[x]);
}
int join(int x, int y){
x = find(x);
y = find(y);
if(x != y){
per[x] = y;
return 1;
}
else
return 0;
}
int main (){
while(scanf("%d", &n) != EOF){
int k = 0;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j){
scanf("%d", &map[++k].w);
map[k].u = i, map[k].v = j;
}
//printf("%d\n",k);
sort(map + 1, map + 1 + k, cmp);
int sum = 0;
init();
for(int i = 1 + n; i <= k; ++i){
//printf("--%d %d %d\n",map[i].u, map[i].v, map[i].w);
if(join(map[i].u, map[i].v))
sum += map[i].w;
}
printf("%d\n", sum);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 1258【最小生成树 && Kruskal && 水题】
标签:
原文地址:http://blog.csdn.net/hpuhjh/article/details/47399231