标签:des style blog os io 数据 for 2014
3 3 1 2 1 1 3 2 2 3 4 1 3 2 3 2 0 100
3 ?
很水的最小生成树。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stack>
#define lson o<<1, l, m
#define rson o<<1|1, m+1, r
using namespace std;
typedef long long LL;
const int maxn = 105;
const int mod = 1000000007;
int n, m, fa[maxn];
struct C {
int st, en, v;
}ed[maxn];
bool cmp(C x, C y) {
return x.v < y.v;
}
int Find(int x) {
return x == fa[x] ? x : x = Find(fa[x]);
}
int main()
{
while(~scanf("%d%d", &n, &m) && n) {
for(int i = 1; i <= n; i++ ) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
ed[i].st = a;
ed[i].en = b;
ed[i].v = c;
}
sort(ed+1, ed+n+1, cmp);
int cnt = 0, sum = 0;
for(int i = 1; i <= m; i++) fa[i] = i;
for(int i = 1; i <= n; i++){
int pres = Find(ed[i].st), pree = Find(ed[i].en);
if(pres != pree) {
fa[pres] = pree;
cnt++;
sum += ed[i].v;
if(cnt == m-1) break;
}
}
if(cnt == m-1) printf("%d\n", sum);
else printf("?\n");
}
return 0;
}
HDU 1863 畅通工程 (最小生成树),布布扣,bubuko.com
标签:des style blog os io 数据 for 2014
原文地址:http://blog.csdn.net/u013923947/article/details/38461711