题目大意:

决定把水灌到n块农田,农田被数字1到n标记

把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库

建造一个水库需要花费wi,连接两块土地需要花费Pij. 计算所需的最少代价

思路:

新开一个节点与每个农田连一条长为wi的边

然后直接最小生成树

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#define inf 2139062143
#define ll long long
#define MAXN 320
#define MOD 1000000007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-;ch=getchar();}
while(isdigit(ch)) {x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,cnt,fa[MAXN];
struct data
{
int u,v,val;
bool operator < (const data &a) const
{
return val<a.val;
}
}e[MAXN*MAXN];
int find(int x) {return x==fa[x]?x:fa[x]=find(fa[x]);}
bool ok(int u,int v)
{
int fu=find(u),fv=find(v);
if(fu==fv) return ;
else {fa[fu]=fv;return ;}
}
void add(int u,int v,int w) {e[++cnt].u=u,e[cnt].v=v,e[cnt].val=w;}
void kruskal()
{
sort(e+,e+cnt+);
int res=,tot=;
for(int i=;i<=cnt;i++)
{
if(!ok(e[i].u,e[i].v)) res+=e[i].val,tot++;
if(tot==n) break;
}
printf("%d",res);
}
int main()
{
n=read();int a,b,c;
for(int i=;i<=n;i++) {a=read(),fa[i]=i;add(i,n+,a);}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
a=read();
if(i!=j) add(i,j,a);
}
kruskal();
}

bzoj 1601 灌水的更多相关文章

  1. 【BZOJ】1601: [Usaco2008 Oct]灌水(kruskal)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1601 很水的题,但是一开始我看成最短路了T_T 果断错. 我们想,要求连通,对,连通!连通的价值最小 ...

  2. BZOJ 1601 [Usaco2008 Oct]灌水

    1601: [Usaco2008 Oct]灌水 Time Limit: 5 Sec  Memory Limit: 162 MB Description Farmer John已经决定把水灌到他的n(1 ...

  3. BZOJ 1601 [Usaco2008 Oct]灌水:最小生成树

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1601 题意: Farmer John已经决定把水灌到他的n(1<=n<=300 ...

  4. BZOJ——1601: [Usaco2008 Oct]灌水

    http://www.lydsy.com/JudgeOnline/problem.php?id=1601 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit:  ...

  5. BZOJ 1601 [Usaco2008 Oct]灌水 (最小生成树)

    题意 Farmer John已经决定把水灌到他的n(1<=n<=300)块农田,农田被数字1到n标记.把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库. 建造一个水库需要 ...

  6. BZOJ 1601 USACO 2008 Oct. 灌水

    [Description] Farmer John已经决定把水灌到他的n(1<=n<=300)块农田,农田被数字1到n标记.把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水 ...

  7. BZOJ 1601: [Usaco2008 Oct]灌水 最小生成树_超级源点

    Description Farmer John已经决定把水灌到他的n(1<=n<=300)块农田,农田被数字1到n标记.把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库. ...

  8. 1601: [Usaco2008 Oct]灌水

    1601: [Usaco2008 Oct]灌水 Time Limit: 5 Sec  Memory Limit: 162 MB Submit: 1342  Solved: 881 [Submit][S ...

  9. BZOJ 1619 [Usaco2008 Nov]Guarding the Farm 保卫牧场:dfs【灌水】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1619 题意: 给你一个n*m的地形图,位置(x,y)的海拔为h[x][y]. 一个山顶的定 ...

随机推荐

  1. Deployd的使用

    deployd一个生成后台数据的软件,可以创建json格式的数据,也可以对数据进行增删改查等操作,甚至可以验证登录,简直就是自学好帮手呀,不用后台搞定后台,就用deployd 下载:链接: https ...

  2. CAD得到范围内实体(网页版)

    主要用到函数说明: IMxDrawSelectionSet::Select 构造选择集.详细说明如下: 参数 说明 [in] MCAD_McSelect Mode 构造选择集方式 [in] VARIA ...

  3. ArrayList经典Demo

    import java.util.ArrayList; import java.util.Iterator; public class ArrayListDemo { public static vo ...

  4. jsp中的basePath,获取应用的路径

    1 2 3 4 5 String path = request.getContextPath();      String basePath = request.getScheme()+": ...

  5. 打造完美的ImageLoader——LruCache+DiskLruCache

    做android应用少不了要和网络打交道,在我刚开始学android的时候总是处理不好网络图片的加载,尤其是图片乱跳的问题,后来发现了各种缓存图片的方法:本地缓存.软引用.LruCache.... 我 ...

  6. css3属性之-webkit-margin-before

    当没有对浏览器进行css边距初始化时,在web-kit浏览器上会出现下面的浏览器默认边距设置: ul, menu, dir { display: block; list-style-type: dis ...

  7. HDU - 2041 - 超级楼梯(dp)

    题意: 有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法? 思路: 如何到第n阶台阶,只能从n-1和n-2台阶上去,那么只需要计算到n-1阶台阶和到n-2阶台 ...

  8. buf.readIntBE()

    buf.readIntBE(offset, byteLength[, noAssert]) buf.readIntLE(offset, byteLength[, noAssert]) offset { ...

  9. Jmeter BeanShell PreProcessor使用笔记

    打印log log.info("content:" + content); 将字符串转化为JsonString import com.alibaba.fastjson.JSON; ...

  10. 使用nfs3将hdfs挂载到本地或远程目录(非kerberos适用)

    最基本的配置方法,aix.kerberos等的操作详见http://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-hdfs/Hdf ...