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