版权声明:转载请注明出处 https://blog.csdn.net/qq_41593522/article/details/84329681
题意
在一个点打井cost[i],连接两个点dis[i][j],求使所有点间接或直接与水井连通的最小花费
题解
对于在一个点打井,视为与0节点连一条边,然后krusual即可
调试记录
无
#include <cstdio>
#include <algorithm>
#define maxn 305
using namespace std;
struct node{
int u, v, l;
}e[maxn * maxn << 2];
int tot = 0;
void addedge(int u, int v, int l){e[++tot] = (node){u, v, l};}
bool cmp(const node &a, const node &b){return a.l < b.l;}
int n, f[maxn];
int getf(int x){return (f[x] == x) ? x : f[x] = getf(f[x]);}
int Krusual(){
sort(e + 1, e + tot + 1, cmp);
for (int i = 1; i <= n; i++) f[i] = i;
int res = 0;
for (int i = 1; i <= tot; i++){
if (getf(e[i].u) != getf(e[i].v)){
f[getf(e[i].u)] = getf(e[i].v);
res += e[i].l;
}
}
return res;
}
int main(){
scanf("%d", &n);
for (int x, i = 1; i <= n; i++){
scanf("%d", &x);
addedge(0, i, x);
}
for (int x, i = 1; i <= n; i++)
for (int j = 1; j <= n; j++){
scanf("%d", &x);
addedge(i, j, x);
}
printf("%d\n", Krusual());
return 0;
}