题面在这里
偶尔刷道水题有益身心健康
做法:
把打井这个操作看成向
n+1
这个点连一条边。问题转化为求
1
~
/*************************************************************
Problem: bzoj 1601 [Usaco2008 Oct]灌水
User: fengyuan
Language: C++
Result: Accepted
Time: 96 ms
Memory: 2728 kb
Submit_Time: 2017-12-15 14:10:29
*************************************************************/
#include<bits/stdc++.h>
#define rep(i, x, y) for (int i = (x); i <= (y); i ++)
#define down(i, x, y) for (int i = (x); i >= (y); i --)
#define mid ((l+r)/2)
#define lc (o<<1)
#define rc (o<<1|1)
#define pb push_back
#define mp make_pair
#define PII pair<int, int>
#define F first
#define S second
#define B begin()
#define E end()
using namespace std;
typedef long long LL;
//head
const int N = 350;
int n, m;
int fa[N];
struct Node{ int x, y, z; }e[N*N];
const bool cmp(const Node &a, const Node &b){ return a.z < b.z; }
inline int getFa(int v){ return fa[v] == v ? v : fa[v] = getFa(fa[v]); }
int main()
{
scanf("%d", &n); int S = n+1;
rep(i, 1, n){
int x; scanf("%d", &x);
e[++ m] = Node{S, i, x};
}
rep(i, 1, n) rep(j, 1, n){
int x; scanf("%d", &x);
if (i < j) e[++ m] = Node{i, j, x};
}
sort(e+1, e+1+m, cmp); int sum = 0, ans = 0;
rep(i, 1, S) fa[i] = i;
rep(i, 1, m){
if (sum == n) break;
int fx = getFa(e[i].x), fy = getFa(e[i].y);
if (fx != fy){
sum ++; ans += e[i].z;
fa[fx] = fy;
}
}
printf("%d\n", ans);
return 0;
}