CCF 201412-4 最优灌溉 传送门
以前的CCF第四题居然这么水, 出了一道裸的最小生成树. 写了6分钟就100分了. 光看题目, 最优灌溉
, 还以为是网络流问题.
辣鸡代码, 不值一看
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct Edge {
int from, to, cost;
Edge () {}
Edge (int f, int t, int c) : from(f), to(t), cost(c) {}
};
const int maxn = 1005;
int n, m, ans = 0, cnt = 0;
vector<Edge> edge;
int father[maxn];
bool cmp(Edge x, Edge y)
{
return x.cost < y.cost;
}
int find(int x)
{
return x == father[x] ? x:father[x] = find(father[x]);
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; ++i)
father[i] = i;
for (int i = 0, u, v, c; i < m; ++i) {
cin >> u >> v >> c;
edge.push_back(Edge(u, v, c));
}
sort(edge.begin(), edge.end(), cmp);
for (int i = 0; i < edge.size(); ++i) {
int x = find(edge[i].from), y = find(edge[i].to);
int cost = edge[i].cost;
if (x != y) {
ans += cost;
cnt++;
father[x] = y;
}
if (cnt == n - 1) {
cout << ans;
return 0;
}
}
}