#include <bits/stdc++.h> #define IOS ios::sync_with_stdio(false);cin.tie(0); using namespace std; typedef long long ll; const int INF = 0x3f3f3f3f; const int MAXN = 1e3 + 5; //先初始化c[][]为INF再输入边权 int c[MAXN][MAXN], lc[MAXN]; bool vis[MAXN]; int prim(int n) { int ans = 0; memset(vis, 0, sizeof(vis)); vis[0] = 1; for (int i = 1; i < n; i++) lc[i] = c[0][i]; for (int i = 1; i < n; i++) { int minc = INF, p = -1; for (int j = 0; j < n; j++) { if (!vis[j] && minc > lc[j]) { minc = lc[j]; p = j; } } //原图不连通 if (minc == INF) return -1; ans += minc; vis[p] = 1; //更新lc[] for (int j = 0; j < n; j++) if (!vis[j] && lc[j] > c[p][j]) lc[j] = c[p][j]; } return ans; }