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算法 ...
随机推荐
- 压缩归档tar命令
归档有两个命令,一个是tar命令,一个是cpio 归档不删除原文件 tar命令语法: tar cvf aa.tar file1 file2 file3 file4 file5 # tar cvf bb ...
- 长沙雅礼中学集训-------------------day1(内含day0)
day0: 首先,请允许我吐槽一下: 1.那些一个人住一个标准房的人您们真的是#@**¥&%……#*()%……*()@Q$&. 2.感谢那些一个人住一个标准间的人,要不然我们也找不到这 ...
- 【BZOJ】1101 [POI2007]Zap(莫比乌斯反演)
题目 传送门:QWQ 分析 莫比乌斯反演. 还不是很熟练qwq 代码 //bzoj1101 //给出a,b,d,询问有多少对二元组(x,y)满足gcd(x,y)=d.x<=a,y<=b # ...
- nginx-1.8.1的安装
1.我直接切换到root用户下安装,这里需要三个插件一起配套使用的 分别是: 1.gzip 模块需要 zlib 库 ( 下载: http://www.zlib.NET/ )2.rewrite 模块需要 ...
- Deep Reinforcement Learning 基础知识
Introduction 深度增强学习Deep Reinforcement Learning是将深度学习与增强学习结合起来从而实现从Perception感知到Action动作的端对端学习的一种全新的算 ...
- smb.conf详解[未完]
看着玩意看的吐血!!!! baidu\google充斥着一堆错误的文章及翻译,samba.org上动辄就是this document is old and might be incurrent. 不过 ...
- 《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #17 如何使用ext4
HACK #17 如何使用ext4 本节介绍ext4的编写和挂载方法.开发版ext4的使用方法.ext4是ext3的后续文件系统,从Linux 2.6.19开始使用.现在主要的发布版中多数都是采用ex ...
- jshint在bat批处理中闪退,代码中无法调用的问题
先说解决办法:加个call eg: call jshint --version Pause 具体原因有空再更
- html-字体
字体大小 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- centos上自动执行脚本执行php文件
1 先编写执行PHP文件的脚本 vi php.sh #!/bin/sh /usr/bin/php /etc/1.php 2把php.sh添加到自动执行任务中 cd /etc/ vi crontab * ...