TZOJ 2754 Watering Hole(最小生成树Kruskal)
描述
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.
输入
- Line 1: A single integer: N
- Lines 2..N + 1: Line i+1 contains a single integer: W_i
- Lines N+2..2N+1: Line N+1+i contains N space-separated integers; the j-th integer is P_ij
输出
- Line 1: A single line with a single integer that is the minimum cost of providing all the pastures with water.
样例输入
4
5
4
4
3
0 2 2 2
2 0 3 3
2 3 0 4
2 3 4 0
样例输出
9
提示
INPUT DETAILS:
There are four pastures. It costs 5 to build a well in pasture 1,4 in pastures 2 and 3, 3 in pasture 4. Pipes cost 2, 3, and 4depending on which pastures they connect.
OUTPUT DETAILS:
Farmer John may build a well in the fourth pasture and connect each pasture to the first, which costs 3 + 2 + 2 + 2 = 9.
题意
农夫要打若干井,和挖连通两个田的水路,求所有田都有水的最小花费
题解
由于有一个挖井系统且必须要挖1个井,所以可以把井看成1个图上的点,然后跑一遍最小生成树即可
代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; #define MAXN 300+5
#define MAXM 90000
int n,m,F[MAXN],Vis[MAXN];
struct edge
{
int u,v,w;
}edges[MAXM];
int Find(int x)
{
return F[x]==-?x:F[x]=Find(F[x]);
}
bool cmp(edge a,edge b)
{
return a.w<b.w;
}
int Kruskal()
{
memset(F,-,sizeof(F));
sort(edges,edges+m,cmp);
int ans=,cnt=,u,v,w,fx,fy;
for(int i=;i<m;i++)
{
u=edges[i].u;
v=edges[i].v;
w=edges[i].w;
fx=Find(u);
fy=Find(v);
if(fx!=fy)
{
ans+=w;
cnt++;
F[fx]=fy;
}
if(cnt==n)break;//连通田n-1条边,加井1条边
}
return ans;
}
void addedges(int u,int v,int w)
{
edges[m].u=u;
edges[m].v=v;
edges[m++].w=w;
}
int main()
{
int w;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&w);
addedges(,i,w);
}
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
scanf("%d",&w);
if(i>=j)continue;
addedges(i,j,w);
}
}
int ans=Kruskal();
printf("%d\n",ans);
return ;
}
TZOJ 2754 Watering Hole(最小生成树Kruskal)的更多相关文章
- Kruskal || BZOJ 1601: [Usaco2008 Oct]灌水 || Luogu P1550 [USACO08OCT]打井Watering Hole
题面:P1550 [USACO08OCT]打井Watering Hole 题解:无 代码: #include<cstdio> #include<cstring> #includ ...
- p1221网络布线(最小生成树 Prim(普里母)算法) p1222 Watering Hole
描述 Description 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当然,他需要你的帮助.约翰已经给他的农场安排了一条高速的网络线路,他想把这条线路 ...
- [USACO08OCT]Watering Hole
[USACO08OCT]Watering Hole 题目大意: Farmer John 有\(n(n\le300)\)个牧场,他希望灌溉他的所有牧场.牧场编号为\(1\sim n\),要灌溉一个牧场有 ...
- Luogu P1550 打井Watering Hole
P1550 [USACO08OCT]打井Watering Hole 题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to ...
- bzoj1601 / P1550 [USACO08OCT]打井Watering Hole(堆优化prim)
P1550 [USACO08OCT]打井Watering Hole 对于自己建水库的情况,新建一个虚拟结点,和其他点的边权即为自建水库的费用 这样问题就转化为一个裸最小生成树问题了. 这里用堆优化 ...
- 模板——最小生成树kruskal算法+并查集数据结构
并查集:找祖先并更新,注意路径压缩,不然会时间复杂度巨大导致出错/超时 合并:(我的祖先是的你的祖先的父亲) 找父亲:(初始化祖先是自己的,自己就是祖先) 查询:(我们是不是同一祖先) 路径压缩:(每 ...
- 洛谷P1550 [USACO08OCT]打井Watering Hole
P1550 [USACO08OCT]打井Watering Hole 题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to ...
- 最小生成树——Kruskal与Prim算法
最小生成树——Kruskal与Prim算法 序: 首先: 啥是最小生成树??? 咳咳... 如图: 在一个有n个点的无向连通图中,选取n-1条边使得这个图变成一棵树.这就叫“生成树”.(如下图) 每个 ...
- 【转】最小生成树——Kruskal算法
[转]最小生成树--Kruskal算法 标签(空格分隔): 算法 本文是转载,原文在最小生成树-Prim算法和Kruskal算法,因为复试的时候只用到Kruskal算法即可,故这里不再涉及Prim算法 ...
随机推荐
- 解决“Can't bind to local 8630 for debugger”错误--查杀多余进程
Can't bind to local 8630 for debugger 表明本地8630端口被占用 1.Windows平台 在windows命令行窗口下执行: 1.查看所有的端口占用情况 C:\& ...
- TensorFlow利用A3C算法训练智能体玩CartPole游戏
本教程讲解如何使用深度强化学习训练一个可以在 CartPole 游戏中获胜的模型.研究人员使用 tf.keras.OpenAI 训练了一个使用「异步优势动作评价」(Asynchronous Advan ...
- python 之路06day
一 字符编码 1 字符编码的定义: 计算机要想工作必须通电,即用‘电’驱使计算机干活,也就是说‘电’的特性决定了计算机的特性.电的特性即高低电平(人类从逻辑上将二进制数1对应高电平,二进制数0 ...
- text-overflow:ellipsis ,溢出文本显示省略号
text-overflow:ellipsis 对溢出文本显示省略号有两个好处, 一是不用通过程序限定字数 二是有利于SEO. 需要使用对对溢出文本显示省略号的通常是文章标题列表,这样处理对搜索引擎更友 ...
- PHP过滤各种HTML标签的表达式,值得收藏
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- zabbix监控windows用户登陆情况
https://yq.aliyun.com/articles/511381 添加登录失败监控项: 特别注意:把类型设置为:文本格式,否则会报类型错误. eventlog[Security,," ...
- 4. java乱码处理
//返回到jsp页面 //request.setCharacterEncoding("utf-8"); //response.setContentType("text/h ...
- Devexpress 百分号显示格式
百分号:{0:P}表示显示为百分号模式.如数据源中为0.5.表示出来为50%
- DataSnap Server HTTP json格式修改 返回图片
DataSnap Server HTTP json 格式修改 http://127.0.0.1:8080/datasnap/rest/TServerMethods1/EchoString/hello ...
- myeclipse 代码提示
from http://fuyiyuan2011.iteye.com/blog/1258264 在软件开发过程中,有了代码提示能使开发能够更加快捷与便利.但在Eclipse ,MyEclipse等ja ...