You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.0 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 2010, M = 10010;
int n, m, p[N];
struct Edge {
2 years ago
int a, b, c;
2 years ago
bool operator<(const Edge &t) const {
2 years ago
return c < t.c;
2 years ago
}
2 years ago
} edge[M];
2 years ago
int el;
int find(int x) {
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
int res;
int main() {
cin >> n >> m;
// 初始化并查集
for (int i = 1; i <= n; i++) p[i] = i;
for (int i = 0; i < m; i++) {
int a, b, c, t;
cin >> t >> a >> b >> c;
if (t == 1) // 必选的加入到同一个并查集
p[find(a)] = find(b), res += c;
else
// 记录可选边有哪些
2 years ago
edge[el++] = {a, b, c};
2 years ago
}
// 对可选边进行由小到大排序
2 years ago
sort(edge, edge + el);
2 years ago
// 枚举每条可选边
for (int i = 0; i < el; i++) {
2 years ago
int a = find(edge[i].a), b = find(edge[i].b), c = edge[i].c;
2 years ago
if (a != b) p[a] = b, res += c;
}
// 输出最短长度
2 years ago
cout << res << endl;
2 years ago
return 0;
}