[USACO] 打井 Watering Hole
题目描述
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 需要为使所有农场都与有水的农场相连或拥有水井所需要的钱数。
题目解析
一眼看上去很麻烦,但是只要想到了其实很简单,最小生成树而已。
加一个点作为井,跑最小生成树,虽然不是很快但对于这道题绰绰有余了(110ms / 10*1s)
思路好题
Code
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std; const int MAXN = + ; struct Edge {
int from,to;
int w;
friend bool operator < (Edge x,Edge y) {
return x.w < y.w;
}
} l[MAXN*MAXN]; int n,ans;
int fa[MAXN];
int cnt; inline int find(int x) {
if(fa[x] == x) return x;
return fa[x] = find(fa[x]);
} inline void add(int x,int y,int z) {
cnt++;
l[cnt].from = x;
l[cnt].to = y;
l[cnt].w = z;
return;
} int main() {
scanf("%d",&n);
int x;
for(int i = ;i <= n;i++) {
scanf("%d",&x);
add(i,n+,x);
}
for(int i = ;i <= n;i++) {
for(int j = ;j <= n;j++) {
scanf("%d",&x);
if(i == j) continue;
add(i,j,x);
}
}
for(int i = ;i <= n+;i++) fa[i] = i;
sort(l+,l++n*n);
int num = ;
for(int i = ;i <= n*n && num <= n;i++) {
if(find(l[i].from) == find(l[i].to)) continue;
num++; ans += l[i].w;
fa[find(l[i].from)] = find(l[i].to);
}
printf("%d\n",ans);
return ;
}
[USACO] 打井 Watering Hole的更多相关文章
- 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 对于自己建水库的情况,新建一个虚拟结点,和其他点的边权即为自建水库的费用 这样问题就转化为一个裸最小生成树问题了. 这里用堆优化 ...
- 洛谷P1550 [USACO08OCT]打井Watering Hole
P1550 [USACO08OCT]打井Watering Hole 题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to ...
- Luogu P1550 打井Watering Hole
P1550 [USACO08OCT]打井Watering Hole 题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to ...
- 题解——洛谷P1550 [USACO08OCT]打井Watering Hole(最小生成树,建图)
题面 题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to his N (1 <= N <= 300) pas ...
- usaco oct09 Watering Hole
Farmer John希望把水源引入他的N (1 <= N <= 300) 个牧场,牧场的编号是1~N.他将水源引入某个牧场的方法有两个,一个是在牧场中打一口井,另一个是将这个牧场与另一个 ...
- luogu P1550 [USACO08OCT]打井Watering Hole
题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to his N (1 <= N <= 300) pastur ...
- 洛谷 题解 P1550 【[USACO08OCT]打井Watering Hole】
本题看似很难,实际上思路非常简单--如果你想通了. 首先有一个问题:图中有几个点?大部分的人会回答\(n\)个点.错了,有\(n+1\)个. 多出来的那个点在哪?关键在于你要理解每一个决策的意义.实际 ...
- 【学术篇】洛谷1550——打井Watering Hole
题目の传送门:https://www.luogu.org/problem/show?pid=1550 精简版题意(本来就精简了不是么):n个点,每个点可以选择打井或从别的有水的点引水,求所有点都有水用 ...
随机推荐
- vijos - P1077克隆龙 (找规律 + 指数型母函数 + python)
P1077克隆龙 Accepted 标签:[显示标签] 描写叙述 如今龙的克隆已成为可能,龙基因由ACTG字母组成,而龙的基因有例如以下特点: 1.A在基因中的出现为偶数次(包含0): 2.C的情况也 ...
- Codeforces Round #311 (Div. 2) D - Vitaly and Cycle
D. Vitaly and Cycle time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 栗染-github中搭建博客遇到的问题之一
运行命令:git push -u origin master To https://github.com/xuzhezhaozhao/Practice.git ! [rejected] master ...
- 个人微信号二次开发SDK协议,个人微信号二次开发api接口
通过这个API接口可以做什么? 通过我们提供的API接口您可以开发: 工作手机(如:X创,X码,XX管家等) 微信群讲课软件(如:讲课X师,一起X堂等) 微信社群管理软件(如:小X管家,微X助手等) ...
- [POI2013]POL-Polarization
题目描述 Everyone knew it would only be a matter of time. So what? Faced for years on, a peril becomes t ...
- GIT学习之路第一天 简介及其安装
本文参考廖雪峰老师的博客进行总结,完整学习请转廖雪峰博客 Git是什么? Git是目前世界上最先进的分布式版本控制系统(没有之一). Git有什么特点?简单来说就是:高端大气上档次! 那什么是版本控制 ...
- hash+set Codeforces Round #291 (Div. 2) C. Watto and Mechanism
题目传送门 /* hash+set:首先把各个字符串的哈希值保存在set容器里,然后对于查询的每一个字符串的每一位进行枚举 用set的find函数查找是否存在替换后的字符串,理解后并不难.另外,我想用 ...
- Traceback (most recent call last): File "setup.py", line 22, in <module> execfile(join(CURDIR, 'src', 'SSHLibrary', 'version.py')) NameError: name 'execfile' is not defined
在python3环境下安装robotframework-SSHLibraray报错: Traceback (most recent call last): File "setup.py&qu ...
- 数据返回(数据共享,即从后端返回到前端调用,四种(requesst、ModelAndView、Model、Map))
@Controller @RequestMapping("/view")//请求父路径 public class GoodsController { @RequestMapping ...
- TCP流量控制与拥塞解决
滑动窗口 但要提高网络利用率: nagle算法 - 延迟 慢启动.拥塞避免 发送端主导cwnd init set ssthresh & cwnd = swnd loop : 网不阻塞 ...