Luogu P1550 打井Watering Hole
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)。
输出格式:
只有一行,为一个整数,表示所需要的钱数。
输入输出样例
4
5
4
4
3
0 2 2 2
2 0 3 3
2 3 0 4
2 3 4 0
9<br>
说明
John等着用水,你只有1s时间!!!
好吧,原谅我太弱了,我也是看的题解,直接被泥萌的脑洞给吓着了。
$$%%%$$
居然是把打井的点看做是与农夫山泉农夫三拳连起来,就是和0号节点连边,然后直接就是最小生成树的模板题了。
脑洞脑洞
上代码
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio> using namespace std; int n, xxx, cnt, ans, tot, f[307]; bool vis[307][307]; struct edge{int u, v, w;}ed[301*301]; int find(int x) {
if(f[x] == x) return x;
else return f[x] = find(f[x]);
} bool cmp(edge a, edge b) {
return a.w < b.w;
} int main() {
scanf("%d", &n);
for(int i=1; i<=n; i++) {scanf("%d", &ed[++cnt].w); ed[cnt].u = 0, ed[cnt].v = i;}
for(int i=1; i<=n; i++) f[i] = i;
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
scanf("%d", &xxx);
if(i != j&&!vis[i][j]&&!vis[j][i]) {
ed[++cnt].u = i, ed[cnt].v = j, ed[cnt].w = xxx;
vis[i][j] = 1, vis[j][i] = 1;
}
}
}
sort(ed+1, ed+1+cnt, cmp);
for(int i=1; i<=cnt; i++) {
int x = find(ed[i].u), y = find(ed[i].v);
if(x != y) {
f[x] = find(y);
ans += ed[i].w;
tot++;
}
if(tot == n) break;
}
printf("%d", ans);
return 0;
}
Luogu P1550 打井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 [USACO08OCT]打井Watering Hole
题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to his N (1 <= N <= 300) pastur ...
- 题解——洛谷P1550 [USACO08OCT]打井Watering Hole(最小生成树,建图)
题面 题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to his N (1 <= N <= 300) pas ...
- 洛谷 题解 P1550 【[USACO08OCT]打井Watering Hole】
本题看似很难,实际上思路非常简单--如果你想通了. 首先有一个问题:图中有几个点?大部分的人会回答\(n\)个点.错了,有\(n+1\)个. 多出来的那个点在哪?关键在于你要理解每一个决策的意义.实际 ...
- 题解 P1550 【[USACO08OCT]打井Watering Hole】
题面(翻译有点问题,最后一句话) 农民John 决定将水引入到他的n(1<=n<=300)个牧场.他准备通过挖若 干井,并在各块田中修筑水道来连通各块田地以供水.在第i 号田中挖一口井需要 ...
- P1550 [USACO08OCT]打井Watering Hole
题目描述 Farmer John has decided to bring water to his N (1 <= N <= 300) pastures which are conven ...
- 【学术篇】洛谷1550——打井Watering Hole
题目の传送门:https://www.luogu.org/problem/show?pid=1550 精简版题意(本来就精简了不是么):n个点,每个点可以选择打井或从别的有水的点引水,求所有点都有水用 ...
随机推荐
- vim随想笔记(1)
本人是一个vim的狂热粉丝,越是使用vim,越是认为琐碎内容太多,时不时地出现一些自己没有见过的使用方法.命令. 因此准备在博客上用空余时间在阅读<学习vi和vim编辑器>的基础上总结一下 ...
- 測试赛D - The War(有控制范围的贪心)
D - The War Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit St ...
- 使用JDBC处理MySQL大文本和大数据
LOB,Large Objects,是一种用于存储大对象的数据类型,一般LOB又分为BLOB与CLOB.BLOB通常用于存储二进制数据,比如图片.音频.视频等.CLOB通常用于存储大文本,比如小说. ...
- MySQL max_connections 总是 214 。不能设大了? max_connections = 214
MySQL max_connections 总是 214 .不能设大了? centos7 mariadb 修改 max_connections 总是214 It was indeed limits s ...
- ArgumentError: You need to supply at least one validatio
创建: 2017/10/02 意思: validate没有内容 例: validates :title
- javascript的鼠标事件---基础
- pcntl研究
虽说php用于并发计算有点山寨,但总比没有强把.(有问题请指正) 下面是pcntl多线程的例子.(只能用于cli模式,而且只能用于linux环境) <?php $starttime=microt ...
- 【NOIP练习赛】开车
[NOIP练习赛]T2.开车 Description 老司机小 Q 要在一个十字路口指挥车队开车,这个十字路口可 以描述为一个 n*n 的矩阵,其中第 2 行到第 n-1 行都各有一道横向车 道,第 ...
- Mac OS安装octave出现的问题-'error:terminal type set to 'unknown'的解决'
学习Machine learning需要使用Octave语言,毕竟Andrew Ng (恩达.吴)力荐.本机系统Mac OS X EI Capitan, 其实什么系统都无所谓了,安装原理都是一样的. ...
- Unity Android交互过坑指南
Unity Android交互过坑指南 介于网上看过很多unity和Android交互的教程,都或多或少的漏掉了一些部分,导致编译过程中出现各种问题,特此整理一份教程,仅供参考 介绍 本次实现的是在游 ...