【BZOJ】1601: [Usaco2008 Oct]灌水(kruskal)
http://www.lydsy.com/JudgeOnline/problem.php?id=1601
很水的题,但是一开始我看成最短路了T_T
果断错。
我们想,要求连通,对,连通!连通的价值最小!当然是生成树!最小生成树!
边的还好做,但是这题有点,怎么办呢?
因为点在图中也起到连通作用,我们加个附加源,向每个点连边,价值为对应点权。
!
然后就ok了。。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | #include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream> #include <algorithm> using namespace std; #define rep(i, n) for(int i=0; i<(n); ++i) #define for1(i,a,n) for(int i=(a);i<=(n);++i) #define for2(i,a,n) for(int i=(a);i<(n);++i) #define for3(i,a,n) for(int i=(a);i>=(n);--i) #define for4(i,a,n) for(int i=(a);i>(n);--i) #define CC(i,a) memset(i,a,sizeof(i)) #define read(a) a=getint() #define print(a) printf("%d", a) #define dbg(x) cout << #x << " = " << x << endl #define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; } inline const int getint() { int r=0, k=1; char c= getchar (); for (; c< '0' ||c> '9' ; c= getchar ()) if (c== '-' ) k=-1; for (; c>= '0' &&c<= '9' ; c= getchar ()) r=r*10+c- '0' ; return k*r; } inline const int max( const int &a, const int &b) { return a>b?a:b; } inline const int min( const int &a, const int &b) { return a<b?a:b; } const int N=500; int p[N], cnt, n; struct ED { int u, v, w; } e[N*N+N]; const bool cmp( const ED &a, const ED &b) { return a.w<b.w; } void add( const int &u, const int &v, const int &w) { e[++cnt].u=u; e[cnt].v=v; e[cnt].w=w; } const int ifind( const int &x) { return x==p[x]?x:p[x]=ifind(p[x]); } int main() { read(n); for1(i, 1, n) add(0, i, getint()); int fx, fy, ans=0; for1(i, 1, n) for1(j, 1, n) { read(fx); if (i!=j) add(i, j, fx); } sort(e+1, e+1+cnt, cmp); for1(i, 1, n) p[i]=i; for1(i, 1, cnt) { fx=ifind(e[i].u); fy=ifind(e[i].v); if (fx!=fy) { p[fx]=fy; ans+=e[i].w; } } print(ans); return 0; } |
Description
Farmer John已经决定把水灌到他的n(1<=n<=300)块农田,农田被数字1到n标记。把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库。 建造一个水库需要花费wi(1<=wi<=100000),连接两块土地需要花费Pij(1<=pij<=100000,pij=pji,pii=0). 计算Farmer John所需的最少代价。
Input
*第一行:一个数n *第二行到第n+1行:第i+1行含有一个数wi *第n+2行到第2n+1行:第n+1+i行有n个被空格分开的数,第j个数代表pij。
Output
*第一行:一个单独的数代表最小代价.
Sample Input
4
5
4
4
3
0 2 2 2
2 0 3 3
2 3 0 4
2 3 4 0
5
4
4
3
0 2 2 2
2 0 3 3
2 3 0 4
2 3 4 0
Sample Output
9
输出详解:
Farmer John在第四块土地上建立水库,然后把其他的都连向那一个,这样就要花费3+2+2+2=9
输出详解:
Farmer John在第四块土地上建立水库,然后把其他的都连向那一个,这样就要花费3+2+2+2=9
HINT
Source
博客地址:www.cnblogs.com/iwtwiioi 本文为博主原创文章,未经博主允许不得转载。一经发现,必将追究法律责任。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步