prim 板子
#include <bits/stdc++.h>
using namespace std;
const int N = 510;
const int inf = 0x3f3f3f3f;
int g[N][N];
int dist[N];
bool st[N];
int n, m;
int prim() {
memset(dist, inf, sizeof dist);
int ans = 0;
// 首先选择1号作为根节点
st[1] = true;
for (int j = 1; j <= n; ++j) dist[j] = min(dist[j], g[1][j]);
// 再选择n - 1个结点
for (int i = 1; i < n; ++i) {
int t = 0;
for (int j = 1; j <= n; ++j) {
if (!st[j] && dist[j] < dist[t]) {
t = j; // 找到当前未加入集合的最小距离结点
}
}
if (dist[t] == inf) return inf;
ans += dist[t];
st[t] = true;
for (int j = 1; j <= n; ++j) {
dist[j] = min(dist[j], g[t][j]); // 更新到集合的点
}
}
return ans;
}
int main () {
cin >> n >> m;
int a, b, c;
// 邻接矩阵的初始化
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (i == j) g[i][j] = 0;
else g[i][j] = inf;
}
}
for (int i = 0; i < m; ++i) {
cin >> a >> b >> c;
g[a][b] = min(g[a][b], c);
g[b][a] = min(g[b][a], c);
}
int ans = prim();
if (ans == inf) cout << "impossible" << endl;
else cout << ans << endl;
return 0;
}