ZYK_luogu @ 2023-08-08 10:51:37
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
const int N = 5005, INF = 0x3f3f3f3f;
int n, m, g[N][N], dist[N], vis[N];
int prim() {
memset(dist, 0x3f, sizeof dist);
int res = 0;
vis[1] = 1;
for (int i = 0; i < n; i ++) {
int t = -1;
for (int j = 1; j <= n; j ++)
if (!vis[j] && (t == -1 || dist[t] > dist[j]))
t = j;
if (i && dist[t] == INF) return INF;
if (i) res += dist[t];
for (int j = 1; j <= n; j ++)
dist[j] = min(dist[j], g[t][j]);
vis[t] = 1;
}
return res;
}
int main() {
scanf("%d%d", &n, &m);
memset(g, 0x3f, sizeof g);
while (m --) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
g[a][b] = min(g[a][b], c);
g[b][a] = min(g[b][a], c);
}
int k = prim();
if (k == INF) printf("orz");
else printf("%d", k);
return 0;
}
by 123asdewq @ 2023-08-08 11:27:49
数组越界