题意:

300个坑,每个坑能从别的坑引水,或者自己出水,i从j饮水有个代价,每个坑自己饮水也有代价,问让所有坑都有谁的最少代价

思路:

先建一个n的完全图,然后建一个超级汇点,对每个点连w[i],跑mst,这样就能保证所有坑联通,并且至少有一个坑有水

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map> #define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
//#define lowbit(x) ((x)&(-x)) using namespace std; typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL; const db eps = 1e-;
const int mod = 1e9+;
const int maxn = 2e6+;
const int maxm = 2e6+;
const int inf = 0x3f3f3f3f;
const db pi = acos(-1.0); int p[][];
int w[];
int n;
int f[maxn];
int find(int x){
return f[x]==x?x:f[x]=find(f[x]);
}
struct node{
int x, y;
int w;
}edge[maxn];
bool cmp(node a, node b){
return a.w<b.w;
}
int tot;
int add(int x, int y, int w){
edge[++tot].x=x;
edge[tot].y=y;
edge[tot].w=w;
}
int main(){
scanf("%d", &n);
for(int i = ; i <= n; i++){
scanf("%d", &w[i]);
add(,i,w[i]);
}
for(int i = ; i <= n; i++){
for(int j = ; j <= n; j++){
scanf("%d", &p[i][j]);
add(i,j,p[i][j]);
}
}
for(int i = ; i <= n; i++)f[i]=i;
sort(edge+, edge++tot,cmp);
int cnt = ;
int ans = ;
for(int i = ; i <= tot; i++){
int x = edge[i].x;
int y = edge[i].y;
int w = edge[i].w;
int t1 = find(x);
int t2 = find(y);
if(t1 != t2){
f[t1] = t2;
ans+=w;
cnt++;
}
if(cnt==n)break;
}
printf("%d",ans);
return ;
} /*
4
5
4
4
3
0 2 2 2
2 0 3 3
2 3 0 4
2 3 4 0
*/
05-01 22:19