题解——洛谷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个点,每个点可以选择打井或从别的有水的点引水,求所有点都有水用 ...
随机推荐
- Linux 压缩解压缩命令详解
tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个.下面的 ...
- scu 4444 Travel
题意: 一个完全图,有n个点,其中m条边是权值为a的无向边,其它是权值为b的无向边,问从1到n的最短路. 思路: 首先判断1和n被哪种边连通. 如果是被a连通,那么就需要全部走b的边到达n,选择最小的 ...
- greenplum presto impala选型与测评
查看原文请至:https://my.oschina.net/hblt147/blog/1843028
- Base64图片编码原理,base64图片工具介绍,图片在线转换Base64
Base64图片编码原理,base64图片工具介绍,图片在线转换Base64 DataURI 允许在HTML文档中嵌入小文件,可以使用 img 标签或 CSS 嵌入转换后的 Base64 编码,减少 ...
- maven 项目打可执行jar包
昨晚,突然就来了紧急任务. 验签较慢,着手优化,发来一个demo. 首先需要把该demo部署在Linux上. 该项目是maven 项目,所以用maven打个jar包,打完jar包之后, ...
- java根据图片的url地址下载图片到本地
package com.daojia.haobo.aicircle.util; import sun.misc.BASE64Encoder; import java.io.*; import java ...
- javascript 的01
javaScript可以实现验证表单.制作特效等功能,JavaScript的目的主要是一下三点: 1 客户端表单验证2 页面动态效果3 jQuery的基础JavaScript是一种描述性语言,也是一种 ...
- MyEclipse中项目运行时发生了Tomcat报错:[java.lang.OutOfMemoryError: PermGen space]
Tomcat内存溢出,异常信息如下: 十一月 26, 2017 1:52:26 下午 org.apache.catalina.core.ContainerBase$ContainerBackgroun ...
- 机器学习笔记 1 LMS和梯度下降(批梯度下降) 20170617
https://www.cnblogs.com/alexYuin/p/7039234.html # 概念 LMS(least mean square):(最小均方法)通过最小化均方误差来求最佳参数的方 ...
- react将表格动态生成视频列表【代码】【案例】
只需要创建一个表格,id为videos,react就能将这个表格转换成视频列表,并点击自动播放 index.html <!DOCTYPE html> <html> <he ...