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
1.3 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 310;
int n;
int g[N][N];
2 years ago
int dis[N];
2 years ago
bool st[N];
int res; // 最小生成树里面边的长度之和
2 years ago
void prim() {
memset(dis, 0x3f, sizeof dis); // 初始化所有距离为INF
dis[0] = 0; // 超级源点是在生成树中的
2 years ago
for (int i = 0; i <= n; i++) { // 注意这里因为引入了超级源点所以点的个数是n+1
int t = -1;
for (int j = 0; j <= n; j++)
2 years ago
if (!st[j] && (t == -1 || dis[t] > dis[j])) t = j;
if (i) res += dis[t];
2 years ago
// 有超级源点的题,是必然存在最小生成树的
// 注意这里也是需要从0~n共n+1个
2 years ago
for (int j = 0; j <= n; j++)
if (!st[j] && dis[j] > g[t][j])
dis[j] = g[t][j];
st[t] = true;
2 years ago
}
}
int main() {
cin >> n;
// 建立超级源点(0 <-> 1~n ),点权转化为超级源点到此节点的边权
for (int i = 1; i <= n; i++) {
2 years ago
int c;
cin >> c;
g[i][0] = g[0][i] = c;
2 years ago
}
// 本题是按矩阵读入的不是按a,b,c方式读入的
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
cin >> g[i][j];
// 利用prim计算最小生成树
2 years ago
prim();
cout << res << endl;
2 years ago
return 0;
}