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个点,每个点可以选择打井或从别的有水的点引水,求所有点都有水用 ...
随机推荐
- Test While You Sleep (and over Weekends)
Test While You Sleep (and over Weekends) Rajith Attapattu RELAX. I am not referring to offshore deve ...
- timus 1018. Binary Apple Tree
1018. Binary Apple Tree Time limit: 1.0 secondMemory limit: 64 MB Let's imagine how apple tree looks ...
- 曼哈顿距离(坐标投影距离之和)d(i,j)=|X1-X2|+|Y1-Y2|.
曼哈顿距离(坐标投影距离之和)d(i,j)=|X1-X2|+|Y1-Y2|. 我们可以定义曼哈顿距离的正式意义为L1-距离或城市区块距离,也就是在欧几里德空间的固定直角坐标系上两点所形成的线段对轴产生 ...
- eclipse高亮选中属性以及更改颜色
1.显示: 1.1.工具栏里有个黄色小笔的图标,点一下就好了 1.2.打开对话框windows->preference,在左上角输入mark Occurrencs 把右边都选 ...
- Jar包中文乱码问题
项目上遇用winrar修改替换jar中一个中文文件名后出现jar包解压读取错误问题,被这个问题纠缠了两次,都是现场比较情急的情况,于是就研究一下彻底弄清楚这个问题.中间也网上搜过一些内容,但实际测试不 ...
- bzoj4591 [Shoi2015]超能粒子炮·改——组合数学(+求阶乘逆元新姿势)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4591 这题不是很裸啊(所以我就不会了) 得稍微推导一下,看这个博客好了:https://bl ...
- Flume Netcat Source
1.cd /usr/local2/flume/conf sudo vim netcat.conf # Name the components on this agent a1.sources = r1 ...
- SQL使用IN参量不能超过1000的表现形式以及解决办法
如果出现这个错误说明你传的参量是超过了一千个值:列如,你拼接了1001个id: 如何解决那,我这里提供两种方法: 1.每1000条加一个or in 列: 原:select p.* from t_pre ...
- 浅谈IO优化
三层结构 磁盘(存储).VM(卷管理)和文件系统.专有名词不好理解,打个比方说:磁盘就相当于一块待用的空地:LVM相当于空地上的围墙(把空地划分成多个部分):文件系统则相当于每块空地上建的楼房(决定了 ...
- LVS上DR和NAT模式的缺陷
引言 相信一般的小公司用的最多的还是DR和NAT模式,关于DR和NAT模式的原理请看看下图,我们先从lvs的DR和NAT模式特性聊聊一些问题. 问题1.lvs的DR模式和NAT模式核心缺陷有哪些? D ...