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

说明

John等着用水,你只有1s时间!!!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 310
using namespace std;
int n,fa[maxn],num;
struct node{
int from,to,v;
bool operator < (const node &a)const{
return v<a.v;
}
}e[maxn*maxn];
int find(int x){
if(x==fa[x])return x;
return fa[x]=find(fa[x]);
}
int main(){
scanf("%d",&n);
int x;
for(int i=;i<=n;i++)fa[i]=i;
for(int i=;i<=n;i++){
scanf("%d",&x);
e[++num].from=;e[num].to=i;e[num].v=x;
}
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
scanf("%d",&x);
if(i>j)e[++num].from=i,e[num].to=j,e[num].v=x;
}
}
sort(e+,e+num+);
int ans=;
for(int i=;i<=num;i++){
int f1=find(e[i].from),f2=find(e[i].to);
if(f1!=f2){
ans+=e[i].v;
fa[f1]=f2;
}
}
printf("%d",ans);
return ;
}

洛谷P1550 [USACO08OCT]打井Watering Hole的更多相关文章

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

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

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

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

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

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

  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

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

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

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

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

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

  8. Luogu P1550 打井Watering Hole

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

  9. 【题解】洛谷P2914[USACO08OCT]断电Power Failure

    洛谷P2914:https://www.luogu.org/problemnew/show/P2914 哇 这题目在暑假培训的时候考到 当时用Floyed会T掉 看楼下都是用Dijkstra 难道没有 ...

随机推荐

  1. java 调用系统外部的某个程序

    有时候我们java 调用系统外部的某个程序 可能需要调用系统外部的某个程序,此时就可以用Runtime.getRuntime().exec()来调用,他会生成一个新的进程去运行调用的程序. 此方法返回 ...

  2. rpm yum 等命令无响应的解决方法

    yum 安装查询任何东西, rpm 安装查询任何东西,执行后无任何反应,直接卡住,也没任何错误信息给出,只能杀掉进程 # yum install XXXX # yum clean all # rpm ...

  3. dubbo学习 三 dubbox概述

    当当网根据自身的需求,对dubbo进行了扩展就叫成了dubbox.具体的使用方法可以参照官网各种例子:http://dangdangdotcom.github.io/dubbox/   支持rest风 ...

  4. Texture Filter

    [Texture Filter] 我们的纹理是要贴到三维图形表面的,而三维图形上的pixel中心和纹理上的texel中心并不一至(pixel不一定对应texture上的采样中心texel),大小也不一 ...

  5. 【原创】10. MYSQL++ 之 DbDriver

    1. 综述 DbDriver只是对于MYSQL C API的一个非常简单的封装,作者原句是This class does as little as possible to adapt between ...

  6. 仿函数(二、stl中常用仿函数)

    提到C++ STL,首先被人想到的是它的三大组件:Containers, Iterators, Algorithms,即容器,迭代器和算法.容器为用户提供了常用的数据结构,算法大多是独立于容器的常用的 ...

  7. ubuntu 下正确安装android手机驱动

    1. 查看手机ID号. charlesxue@THSHIBA:~/setup/cocos2d-x/cocos2d-x-/projects/simpleGame/proj.android/bin$ ls ...

  8. opencv3 图片模糊操作-均值滤波 高斯滤波 中值滤波 双边滤波

    #include <iostream>#include <opencv2/opencv.hpp> using namespace std;using namespace cv; ...

  9. 453. Minimum Moves to Equal Array Elements 一次改2个数,变成统一的

    [抄题]: Given a non-empty integer array of size n, find the minimum number of moves required to make a ...

  10. 2-JRE System Libraty [eclipse-mars](unbound)