您的位置:首页 > 其它

HDU 1863 畅通工程——最小生成树

2017-05-15 11:32 316 查看
直接用Kruskal算法求解,用cnt记录最小生成树的边数进行判断

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int maxn = 110;

int par[maxn], ran[maxn];

void init(int m) {
for (int i = 0; i <= m; i++) {
par[i] = i, ran[i] = 0;
}
}

int seek(int x) {
if (par[x] == x) return x;
else return par[x] = seek(par[x]);
}

void unite(int x, int y) {
x = seek(x), y = seek(y);
if (x == y) return;
if (ran[x] == ran[y]) par[x] = y;
else {
par[y] = x;
if (ran[x] == ran[y]) ran[x]++;
}
}

struct Edge {
int u, v, cost;
bool operator < (const Edge &another) const {
return cost < another.cost;
}
}edge[maxn*maxn];

int main()
{
int m, n, u, v, cost;
while (scanf("%d %d", &n, &m) == 2 && n) {
for (int i = 1; i <= n; i++) {
scanf("%d %d %d", &u, &v, &cost);
edge[i].u = u, edge[i].v = v, edge[i].cost = cost;
}
sort(edge + 1, edge + 1 + n);
init(m);
int cnt = 0, ans = 0;
for (int i = 1; i <= n; i++) {
if (cnt == m - 1) break;
if (seek(edge[i].u) != seek(edge[i].v)) {
cnt++;
ans += edge[i].cost;
unite(edge[i].u, edge[i].v);
}
}
if (cnt == m - 1) printf("%d\n", ans);
else printf("?\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: