题目描述

Farmer John has decided to bring water to his N (1 <= N <= 300) pastures which are conveniently numbered 1..N. He may bring water to a pasture either by building a well in that pasture or connecting the pasture via a pipe to another pasture which already has water.

Digging a well in pasture i costs W_i (1 <= W_i <= 100,000).

Connecting pastures i and j with a pipe costs P_ij (1 <= P_ij <= 100,000; P_ij = P_ji; P_ii=0).

Determine the minimum amount Farmer John will have to pay to water all of his pastures.

POINTS: 400

农民John 决定将水引入到他的n(1<=n<=300)个牧场。他准备通过挖若干井,并在各块田中修筑水道来连通各块田地以供水。在第i 号田中挖一口井需要花费W_i(1<=W_i<=100,000)元。连接i 号田与j 号田需要P_ij (1 <= P_ij <= 100,000 , P_ji=P_ij)元。

请求出农民John 需要为使所有农场都与有水的农场相连或拥有水井所需要的钱数。

题目解析

一眼看上去很麻烦,但是只要想到了其实很简单,最小生成树而已。

加一个点作为井,跑最小生成树,虽然不是很快但对于这道题绰绰有余了(110ms / 10*1s)

思路好题

Code

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std; const int MAXN = + ; struct Edge {
int from,to;
int w;
friend bool operator < (Edge x,Edge y) {
return x.w < y.w;
}
} l[MAXN*MAXN]; int n,ans;
int fa[MAXN];
int cnt; inline int find(int x) {
if(fa[x] == x) return x;
return fa[x] = find(fa[x]);
} inline void add(int x,int y,int z) {
cnt++;
l[cnt].from = x;
l[cnt].to = y;
l[cnt].w = z;
return;
} int main() {
scanf("%d",&n);
int x;
for(int i = ;i <= n;i++) {
scanf("%d",&x);
add(i,n+,x);
}
for(int i = ;i <= n;i++) {
for(int j = ;j <= n;j++) {
scanf("%d",&x);
if(i == j) continue;
add(i,j,x);
}
}
for(int i = ;i <= n+;i++) fa[i] = i;
sort(l+,l++n*n);
int num = ;
for(int i = ;i <= n*n && num <= n;i++) {
if(find(l[i].from) == find(l[i].to)) continue;
num++; ans += l[i].w;
fa[find(l[i].from)] = find(l[i].to);
}
printf("%d\n",ans);
return ;
}

[USACO] 打井 Watering Hole的更多相关文章

  1. Kruskal || BZOJ 1601: [Usaco2008 Oct]灌水 || Luogu P1550 [USACO08OCT]打井Watering Hole

    题面:P1550 [USACO08OCT]打井Watering Hole 题解:无 代码: #include<cstdio> #include<cstring> #includ ...

  2. bzoj1601 / P1550 [USACO08OCT]打井Watering Hole(堆优化prim)

    P1550 [USACO08OCT]打井Watering Hole   对于自己建水库的情况,新建一个虚拟结点,和其他点的边权即为自建水库的费用 这样问题就转化为一个裸最小生成树问题了. 这里用堆优化 ...

  3. 洛谷P1550 [USACO08OCT]打井Watering Hole

    P1550 [USACO08OCT]打井Watering Hole 题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to ...

  4. Luogu P1550 打井Watering Hole

    P1550 [USACO08OCT]打井Watering Hole 题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to ...

  5. 题解——洛谷P1550 [USACO08OCT]打井Watering Hole(最小生成树,建图)

    题面 题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to his N (1 <= N <= 300) pas ...

  6. usaco oct09 Watering Hole

    Farmer John希望把水源引入他的N (1 <= N <= 300) 个牧场,牧场的编号是1~N.他将水源引入某个牧场的方法有两个,一个是在牧场中打一口井,另一个是将这个牧场与另一个 ...

  7. luogu P1550 [USACO08OCT]打井Watering Hole

    题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to his N (1 <= N <= 300) pastur ...

  8. 洛谷 题解 P1550 【[USACO08OCT]打井Watering Hole】

    本题看似很难,实际上思路非常简单--如果你想通了. 首先有一个问题:图中有几个点?大部分的人会回答\(n\)个点.错了,有\(n+1\)个. 多出来的那个点在哪?关键在于你要理解每一个决策的意义.实际 ...

  9. 【学术篇】洛谷1550——打井Watering Hole

    题目の传送门:https://www.luogu.org/problem/show?pid=1550 精简版题意(本来就精简了不是么):n个点,每个点可以选择打井或从别的有水的点引水,求所有点都有水用 ...

随机推荐

  1. git如何避免每次pull或者push的时候都要输入用户名和密码?

    git config --global credential.helper store 这个命令则是在你的本地生成一个账号密码的本子似的东东,这样就不用每次都输入了(但是还得输入一次) 这个指令对于w ...

  2. oracle-扫盲贴:存储过程实现增删改查

    原文引入:http://blog.csdn.net/yangzhawen/article/details/8617179 oracle-扫盲贴:存储过程实现增删改查 分类: oracle2013-02 ...

  3. 【PA 2014】Kuglarz

    [题目链接]            点击打开链接 [算法]            sum[i]表示前i个杯子中,杯子底下藏有球的杯子总数            那么,知道[i,j]这段区间中,藏有球的 ...

  4. Face alignment at 3000FPS via Regressing Local Binrary features 理解

    这篇是Ren Shaoqing发表在cvpr2014上的paper,论文是在CPR框架下做的,想了解CPR的同学可以参见我之前的博客,网上有同学给出了code,该code部分实现了LBF,链接为htt ...

  5. centOS封装

    前言 在实际工作中,CentOS的安装需要设置的语言.键盘模式.时区等信息都存在很大程度上的雷同型.并且,安装完成后的一些设置工作也都是一样的.这些工作都可以在安装操作系统的时候自动完成.最终做到,安 ...

  6. Scala 归约操作- - - - -reduce

    object 归约操作_reduce { def main(args: Array[String]): Unit = { val list=List(,,,,) val result=list.red ...

  7. iOS开发——多线程

    很多朋友都说iOS开发中,最难理解和学习的就是多线程,很多的原理实现都是通过log看到,也比较抽象,本人也是在多线程方面投入过很多脑细胞..无论这方面的知识掌握和应用起来是否轻松,牢固的基本功.正确的 ...

  8. Java多线程(八) synchronized 抛出异常锁自动解除

    当一个线程执行的代码出现异常时,其所持有的锁会自动释放 public class MyObject { private int i = 1; synchronized public void meth ...

  9. 树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland

    题目传送门 /* 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计 ...

  10. Storm概念学习系列之storm的优化

    不多说,直接上干货!