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)。

输出格式:

只有一行,为一个整数,表示所需要的钱数。

输入输出样例

输入样例#1:

4
5
4
4
3
0 2 2 2
2 0 3 3
2 3 0 4
2 3 4 0

  

输出样例#1:

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的更多相关文章

  1. Kruskal || BZOJ 1601: [Usaco2008 Oct]灌水 || Luogu P1550 [USACO08OCT]打井Watering Hole

    题面:P1550 [USACO08OCT]打井Watering Hole 题解:无 代码: #include<cstdio> #include<cstring> #includ ...

  2. bzoj1601 / P1550 [USACO08OCT]打井Watering Hole(堆优化prim)

    P1550 [USACO08OCT]打井Watering Hole   对于自己建水库的情况,新建一个虚拟结点,和其他点的边权即为自建水库的费用 这样问题就转化为一个裸最小生成树问题了. 这里用堆优化 ...

  3. 洛谷P1550 [USACO08OCT]打井Watering Hole

    P1550 [USACO08OCT]打井Watering Hole 题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to ...

  4. luogu P1550 [USACO08OCT]打井Watering Hole

    题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to his N (1 <= N <= 300) pastur ...

  5. 题解——洛谷P1550 [USACO08OCT]打井Watering Hole(最小生成树,建图)

    题面 题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to his N (1 <= N <= 300) pas ...

  6. 洛谷 题解 P1550 【[USACO08OCT]打井Watering Hole】

    本题看似很难,实际上思路非常简单--如果你想通了. 首先有一个问题:图中有几个点?大部分的人会回答\(n\)个点.错了,有\(n+1\)个. 多出来的那个点在哪?关键在于你要理解每一个决策的意义.实际 ...

  7. 题解 P1550 【[USACO08OCT]打井Watering Hole】

    题面(翻译有点问题,最后一句话) 农民John 决定将水引入到他的n(1<=n<=300)个牧场.他准备通过挖若 干井,并在各块田中修筑水道来连通各块田地以供水.在第i 号田中挖一口井需要 ...

  8. P1550 [USACO08OCT]打井Watering Hole

    题目描述 Farmer John has decided to bring water to his N (1 <= N <= 300) pastures which are conven ...

  9. 【学术篇】洛谷1550——打井Watering Hole

    题目の传送门:https://www.luogu.org/problem/show?pid=1550 精简版题意(本来就精简了不是么):n个点,每个点可以选择打井或从别的有水的点引水,求所有点都有水用 ...

随机推荐

  1. 【JAVA】merge two array by order

    public class MergeSort { static void show(int a[]) { int i; for (i = 0; i < a.length; i++) { Syst ...

  2. 【独立开发人员er Cocos2d-x实战 009】Cocos2dx 菜单项CCMenu使用

    Cocos2dx中的菜单用CCMenu/Menu类来实现.该类是一个容器.用来装载各种菜单项,用于菜单项能够是图片.系统字体等. 理论就不说了.先上代码: CCMenuItemToggle* item ...

  3. unable to execute dex: multiple dex files Cocos2dxAccelerometer

    原文转载:http://discuss.cocos2d-x.org/t/conversion-to-dalvik-format-failed-unable-to-execute-dex-multipl ...

  4. VC 获取任务栏窗体的句柄

     本文将介绍一个未公开的Win32 API函数:GetTaskmanWindow.利用它对Windows的任务栏进行操作. 这个函数返回拥有任务栏button的窗体句柄. 在微软的MSDN文档中. ...

  5. EF:插入记录失败,提示当 IDENTITY_INSERT 设置为 OFF 时,不能向表中的标识列插入显式值

    EF忽然报错:增加记录时,提示当 IDENTITY_INSERT 设置为 OFF 时,不能向表 '...' 中的标识列插入显式值.真奇怪,添加记录方法一向好地地,从没出过错.要出错,那也是Update ...

  6. Codeforces Round #313 A. Currency System in Geraldion(简单题)

    A. Currency System in Geraldion time limit per test 2 seconds memory limit per test 256 megabytes in ...

  7. visual studio推荐的插件

    https://marketplace.visualstudio.com/items?itemName=EricLebetsamer.BootstrapSnippetPack https://mark ...

  8. c3p0-config.xml文件简单说明与备忘

    <?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <named-confi ...

  9. 汇编程序44:检测点13.1 (jmp near ptr 标号指令的中断例程)

    安装程序: assume cs:code //jmp near ptr 标号指令的替代实现,使用iret指令 code segment start: mov ax,cs mov ds,ax mov s ...

  10. 【BZOJ3294/洛谷3158】[CQOI2011]放棋子(组合数+DP)

    题目: 洛谷3158 分析: 某OIer兔崽子的此题代码中的三个函数名:dfs.ddfs.dddfs(充满毒瘤的气息 显然,行与行之间.列与列之间是互相独立的.考虑背包,用\(f[k][i][j]\) ...