题解——洛谷P1550 [USACO08OCT]打井Watering Hole(最小生成树,建图)
题面
题目背景
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)。
输出格式:
只有一行,为一个整数,表示所需要的钱数。
输入输出样例
说明
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(最小生成树,建图)的更多相关文章
- 洛谷P1550 [USACO08OCT]打井Watering Hole
P1550 [USACO08OCT]打井Watering Hole 题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to ...
- Kruskal || BZOJ 1601: [Usaco2008 Oct]灌水 || Luogu P1550 [USACO08OCT]打井Watering Hole
题面:P1550 [USACO08OCT]打井Watering Hole 题解:无 代码: #include<cstdio> #include<cstring> #includ ...
- bzoj1601 / P1550 [USACO08OCT]打井Watering Hole(堆优化prim)
P1550 [USACO08OCT]打井Watering Hole 对于自己建水库的情况,新建一个虚拟结点,和其他点的边权即为自建水库的费用 这样问题就转化为一个裸最小生成树问题了. 这里用堆优化 ...
- luogu P1550 [USACO08OCT]打井Watering Hole
题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to his N (1 <= N <= 300) pastur ...
- P1550 [USACO08OCT]打井Watering Hole
题目描述 Farmer John has decided to bring water to his N (1 <= N <= 300) pastures which are conven ...
- 洛谷 题解 P1550 【[USACO08OCT]打井Watering Hole】
本题看似很难,实际上思路非常简单--如果你想通了. 首先有一个问题:图中有几个点?大部分的人会回答\(n\)个点.错了,有\(n+1\)个. 多出来的那个点在哪?关键在于你要理解每一个决策的意义.实际 ...
- 题解 P1550 【[USACO08OCT]打井Watering Hole】
题面(翻译有点问题,最后一句话) 农民John 决定将水引入到他的n(1<=n<=300)个牧场.他准备通过挖若 干井,并在各块田中修筑水道来连通各块田地以供水.在第i 号田中挖一口井需要 ...
- Luogu P1550 打井Watering Hole
P1550 [USACO08OCT]打井Watering Hole 题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to ...
- 【学术篇】洛谷1550——打井Watering Hole
题目の传送门:https://www.luogu.org/problem/show?pid=1550 精简版题意(本来就精简了不是么):n个点,每个点可以选择打井或从别的有水的点引水,求所有点都有水用 ...
随机推荐
- 转:安装PHP出现make: *** [sapi/cli/php] Error 1 解决办法
ext/iconv/.libs/iconv.o: In function `php_iconv_stream_filter_ctor':/home/king/PHP-5.2.13/ext/iconv/ ...
- devstack 部署 openstack(pick/mitaka)
链接出处: https://blog.csdn.net/qiqishuang/article/details/51990662 报错处理出处:https://blog.csdn.net/wang114 ...
- PCH 文件 和 ProjectName-Bridging-Header 配置
1.简介 PCH文件是Xcode编程中全局引用共享的文件.可以在这里引入头文件或者宏定义来方便程序中多个文件访问. 2.PCH文件创建 打开工程 New File… -> iOS Other - ...
- mysql 问题:wait_timeout
1,show global variables like 'wait_timeout'; 查询'wait_timeout' 默认是8天即28800秒 2,修改 windows 安装目录下的my.ini ...
- SVM支撑向量机原理
转自:http://blog.csdn.net/v_july_v/article/details/7624837 目录(?)[-] 支持向量机通俗导论理解SVM的三层境界 前言 第一层了解SVM 1分 ...
- python 创建二维数组的方法
废话不多说,直接上代码: #coding=utf-8 def two_di_demo1(): a=[] for i in range(10): a.append([]) for j in range( ...
- Selenium自动化测试,接口自动化测试开发,性能测试从入门到精通
Selenium自动化测试,接口自动化测试开发,性能测试从入门到精通Selenium接口性能自动化测试基础部分:分层自动化思想Slenium介绍Selenium1.0/2.0/3.0Slenium R ...
- IE haslayout的属性及其值
haslayout是IE 渲染引擎的一个内部组成部分.在IE 中,一个元素要么自己对自身的内容进行计算大小和组织,要么依赖于父元素来计算尺寸和组织内容.为了调节这两个不同的概念,渲染引擎采用了hasl ...
- The logback manual #02# Architecture
索引 Logback's architecture Logger, Appenders and Layouts Effective Level(有效等级)又名Level Inheritance Ret ...
- asyncio 中给running 的loop 动态添加 Future Task
https://my.oschina.net/backbye/blog/1919486 asyncio 提供了两个给运行中的事件循环(loop) 添加 事件的方法 call_soon_threadsa ...