题面

题目背景

John的农场缺水了!!!

题目描述

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 需要为连通整个牧场的每一块田地所需要的钱数。

输入输出格式

输入格式:

第1 行为一个整数n。

第2 到n+1 行每行一个整数,从上到下分别为W_1 到W_n。

第n+2 到2n+1 行为一个矩阵,表示需要的经费(P_ij)。

输出格式:

只有一行,为一个整数,表示所需要的钱数。

输入输出样例

输入样例#1: 复制

4
5
4
4
3
0 2 2 2
2 0 3 3
2 3 0 4
2 3 4 0
输出样例#1: 复制

9

说明

John等着用水,你只有1s时间!!!

题解

神奇的建图方式,看到条件,首先想到最小生成树

然后发现每个点都有自己的点权(就是打井)

而且最后图还不连通

最小生成树需要联通图,我们要考虑怎么建立图,使其在不丢失信息的前提下使图联通

答案就产生了

设一个超级源,将所有点向它连边,权值为在每个点打井的权值

然后把点与点之间的\( n^{2} \)条边暴力建出,然后就跑一边kruskal

done!

贴代码

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN = ;
const int MAXM = ;
struct E{
int u,v,w;
}edges[MAXM];
int cnt=,first[MAXN],nxt[MAXM],n;
void addedge(int ux,int vx,int wx){
cnt++;
edges[cnt].u=ux;
edges[cnt].v=vx;
edges[cnt].w=wx;
nxt[cnt]=first[ux];
first[ux]=cnt;
}
bool cmp(E a,E b){
if(a.w<b.w)
return true;
else
return false;
}
int fa[MAXN],ans=;
int find(int x){
if(fa[x]==x)
return x;
else
return fa[x]=find(fa[x]);
}
void kruskal(void){
int inq=;
sort(edges+,edges+cnt+,cmp);
for(int i=;i<=cnt;i++){
int um=edges[i].u;
int vm=edges[i].v;
int x=find(um);
int y=find(vm);
if(x==y)
continue;
else{
fa[x]=y;
ans+=edges[i].w;
inq++;
}
if(inq==n)
return;
}
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
int x;
scanf("%d",&x);
addedge(i,n+,x);
addedge(n+,i,x);
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
int x;
scanf("%d",&x);
if(i==j)
continue;
addedge(i,j,x);
}
for(int i=;i<=n+;i++)
fa[i]=i;
kruskal();
printf("%d",ans);
return ;
}

题解——洛谷P1550 [USACO08OCT]打井Watering Hole(最小生成树,建图)的更多相关文章

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

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

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

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

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

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

  4. luogu P1550 [USACO08OCT]打井Watering Hole

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

  5. P1550 [USACO08OCT]打井Watering Hole

    题目描述 Farmer John has decided to bring water to his N (1 <= N <= 300) pastures which are conven ...

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

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

  7. 题解 P1550 【[USACO08OCT]打井Watering Hole】

    题面(翻译有点问题,最后一句话) 农民John 决定将水引入到他的n(1<=n<=300)个牧场.他准备通过挖若 干井,并在各块田中修筑水道来连通各块田地以供水.在第i 号田中挖一口井需要 ...

  8. Luogu P1550 打井Watering Hole

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

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

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

随机推荐

  1. html5-css渐变应用小实例,按钮

    .but1{    padding: 10px 20px;    font-size: 16px;    text-shadow: 2px 2px 3px rgba(0,0,0,0.8);    bo ...

  2. Spring NoSuchBeanDefinitionException

    转http://www.baeldung.com/spring-nosuchbeandefinitionexception 1. Overview In this article, we are di ...

  3. JS-向数组指定位置添加元素

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. Vector集合——单列集合的“祖宗”类

    是实现可增长的对象数组:所以底层也是数组: 与collection集合不同的是,vector是同步的,意味着是单线程的,意味着效率低,速度慢, 所以在jdk1.2版本之后被ArrayList集合所取代 ...

  5. <转>jmeter(九)逻辑控制器

    本博客转载自:http://www.cnblogs.com/imyalost/category/846346.html 个人感觉不错,对jmeter讲解非常详细,担心以后找不到了,所以转发出来,留着慢 ...

  6. Python+OpenCV图像处理(七)—— 滤波与模糊操作

    过滤是信号和图像处理中基本的任务.其目的是根据应用环境的不同,选择性的提取图像中某些认为是重要的信息.过滤可以移除图像中的噪音.提取感兴趣的可视特征.允许图像重采样等等.频域分析将图像分成从低频到高频 ...

  7. html5的理解

    1.良好的移动性,以移动设备为主 2.响应式设计,以适应自动变化的屏幕尺寸 3.支持离线缓存技术,webStorage本地缓存 4.新增canvas.video.audio等新标签元素,新增特殊内容元 ...

  8. sql server 触发器的简单用法

    触发器  -- 一下写的都是我对触发器的理解 当在执行insert . delete . 等操作的时候 随便要做一些额外的操作, 比如在添加的时候就会将新添加的数据存到inserted表中 写个实例 ...

  9. 数据分析之pandas01

    Series 一.Series Series是一种类似于一维数组的对象,有两部分组成:     .values:一组数据(ndarray类型)     .index: 相关的数据索引标签 二.seri ...

  10. css背景样式background

    background用来定义html元素的背景效果 background-color:定义元素的背景颜色,背景的颜色值通常有三种定义方法 1.十六进制方式,如"#ff0000" 2 ...