zoukankan      html  css  js  c++  java
  • 【BZOJ】1601: [Usaco2008 Oct]灌水(kruskal)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1601

    很水的题,但是一开始我看成最短路了T_T

    果断错。

    我们想,要求连通,对,连通!连通的价值最小!当然是生成树!最小生成树!

    边的还好做,但是这题有点,怎么办呢?

    因为点在图中也起到连通作用,我们加个附加源,向每个点连边,价值为对应点权。

    然后就ok了。。

    #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

    Sample Output

    9


    输出详解:

    Farmer John在第四块土地上建立水库,然后把其他的都连向那一个,这样就要花费3+2+2+2=9

    HINT

    Source

  • 相关阅读:
    小甲鱼系列→第一章.基础知识
    FusionCharts-堆栈图、xml格式、刷新数据、添加事件link、传参
    FireBug提示:本页面不包含 JavaScript,明明是包含js的。
    Angular-Chart.js 初接触;;;
    错误 Metadata file 'C:CommoninDebugCommon.dll' could not be found
    UML--PowerDesigner使用小结
    java8入门 错误:找不到或者无法加载主类
    “基础提供程序在Open上失败”
    设计模式--目录开篇
    020医疗项目-模块二:药品目录的导入导出-介绍药品表
  • 原文地址:https://www.cnblogs.com/iwtwiioi/p/3948500.html
Copyright © 2011-2022 走看看