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.

49 lines
983 B

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 110;
2 years ago
int g[N][N];
int dis[N];
2 years ago
bool st[N];
int n, m, sum;
int b[N];
2 years ago
int prim(int s) {
memset(dis, 0x3f, sizeof dis);
dis[s] = 0;
b[s] = 1;
2 years ago
int res = 0;
for (int i = 1; i <= n; i++) {
int t = -1;
for (int j = 1; j <= n; j++)
2 years ago
if (!st[j] && (t == -1 || dis[t] > dis[j]))
2 years ago
t = j;
st[t] = true;
2 years ago
if (dis[t] != INF) res += dis[t], b[t] = 1;
2 years ago
for (int j = 1; j <= n; j++)
2 years ago
dis[j] = min(dis[j], g[t][j]);
2 years ago
}
return res;
}
int main() {
cin >> n >> m;
2 years ago
memset(g, 0x3f, sizeof g);
2 years ago
while (m--) {
int a, b, c;
cin >> a >> b >> c;
2 years ago
g[a][b] = g[b][a] = c;
2 years ago
sum += c; // 总边长
}
int s = 0;
for (int i = 1; i <= n; i++)
if (!b[i]) s += prim(i);
printf("%d\n", sum - s);
return 0;
}