最小生成树 : 虚拟源点
https://www.acwing.com/problem/content/1148/
#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define x first
#define y second
#define inf 0x3f3f3f3f
const int N = 310;
int n;
int w[N][N];
int dist[N];
bool st[N];
int prim() {
memset(dist, 0x3f, sizeof dist);
dist[0] = 0;
int res = 0;
for (int i = 0; i < n + 1; ++i) {
int t = -1;
for (int j = 0; j <= n; ++j)
if (!st[j] && (t == -1 || dist[j] < dist[t]))
t = j;
st[t] = true;
res += dist[t];
for (int j = 0; j <= n; ++j) dist[j] = min(dist[j], w[t][j]);
}
return res;
}
int main() {
IO;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> w[0][i];
w[i][0] = w[0][i];
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
cin >> w[i][j];
cout << prim() << '\n';
return 0;
}