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
1008 B

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 110;
int w[N][N];
int dist[N];
bool st[N];
int n, m, sum;
int b[N];
int prim(int source) {
memset(dist, 0x3f, sizeof dist);
dist[source] = 0;
b[source] = 1;
int res = 0;
for (int i = 1; i <= n; i++) {
int t = -1;
for (int j = 1; j <= n; j++)
if (!st[j] && (t == -1 || dist[t] > dist[j]))
t = j;
st[t] = true;
if (dist[t] != INF) res += dist[t], b[t] = 1;
for (int j = 1; j <= n; j++)
dist[j] = min(dist[j], w[t][j]);
}
return res;
}
int main() {
cin >> n >> m;
memset(w, 0x3f, sizeof w);
while (m--) {
int a, b, c;
cin >> a >> b >> c;
w[a][b] = w[b][a] = c;
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;
}