描述

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.

输入

  • Line 1: A single integer: N
  • Lines 2..N + 1: Line i+1 contains a single integer: W_i
  • Lines N+2..2N+1: Line N+1+i contains N space-separated integers; the j-th integer is P_ij

输出

  • Line 1: A single line with a single integer that is the minimum cost of providing all the pastures with water.

样例输入

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

样例输出

9

提示

INPUT DETAILS:

There are four pastures. It costs 5 to build a well in pasture 1,4 in pastures 2 and 3, 3 in pasture 4. Pipes cost 2, 3, and 4depending on which pastures they connect.

OUTPUT DETAILS:

Farmer John may build a well in the fourth pasture and connect each pasture to the first, which costs 3 + 2 + 2 + 2 = 9.

题意

农夫要打若干井,和挖连通两个田的水路,求所有田都有水的最小花费

题解

由于有一个挖井系统且必须要挖1个井,所以可以把井看成1个图上的点,然后跑一遍最小生成树即可

代码

 #include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; #define MAXN 300+5
#define MAXM 90000
int n,m,F[MAXN],Vis[MAXN];
struct edge
{
int u,v,w;
}edges[MAXM];
int Find(int x)
{
return F[x]==-?x:F[x]=Find(F[x]);
}
bool cmp(edge a,edge b)
{
return a.w<b.w;
}
int Kruskal()
{
memset(F,-,sizeof(F));
sort(edges,edges+m,cmp);
int ans=,cnt=,u,v,w,fx,fy;
for(int i=;i<m;i++)
{
u=edges[i].u;
v=edges[i].v;
w=edges[i].w;
fx=Find(u);
fy=Find(v);
if(fx!=fy)
{
ans+=w;
cnt++;
F[fx]=fy;
}
if(cnt==n)break;//连通田n-1条边,加井1条边
}
return ans;
}
void addedges(int u,int v,int w)
{
edges[m].u=u;
edges[m].v=v;
edges[m++].w=w;
}
int main()
{
int w;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&w);
addedges(,i,w);
}
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
scanf("%d",&w);
if(i>=j)continue;
addedges(i,j,w);
}
}
int ans=Kruskal();
printf("%d\n",ans);
return ;
}

TZOJ 2754 Watering Hole(最小生成树Kruskal)的更多相关文章

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

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

  2. p1221网络布线(最小生成树 Prim(普里母)算法) p1222 Watering Hole

    描述 Description 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当然,他需要你的帮助.约翰已经给他的农场安排了一条高速的网络线路,他想把这条线路 ...

  3. [USACO08OCT]Watering Hole

    [USACO08OCT]Watering Hole 题目大意: Farmer John 有\(n(n\le300)\)个牧场,他希望灌溉他的所有牧场.牧场编号为\(1\sim n\),要灌溉一个牧场有 ...

  4. Luogu P1550 打井Watering Hole

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

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

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

  6. 模板——最小生成树kruskal算法+并查集数据结构

    并查集:找祖先并更新,注意路径压缩,不然会时间复杂度巨大导致出错/超时 合并:(我的祖先是的你的祖先的父亲) 找父亲:(初始化祖先是自己的,自己就是祖先) 查询:(我们是不是同一祖先) 路径压缩:(每 ...

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

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

  8. 最小生成树——Kruskal与Prim算法

    最小生成树——Kruskal与Prim算法 序: 首先: 啥是最小生成树??? 咳咳... 如图: 在一个有n个点的无向连通图中,选取n-1条边使得这个图变成一棵树.这就叫“生成树”.(如下图) 每个 ...

  9. 【转】最小生成树——Kruskal算法

    [转]最小生成树--Kruskal算法 标签(空格分隔): 算法 本文是转载,原文在最小生成树-Prim算法和Kruskal算法,因为复试的时候只用到Kruskal算法即可,故这里不再涉及Prim算法 ...

随机推荐

  1. java-appium-527进阶-1 UiAutomator1&2区别和封装

    1.UiAutomator和UiAtumator2的区别: 1.1 UiAutomator1有关于id定位的策略 UiAutomator1 id定位在resourceid匹配失败时,会匹配conten ...

  2. Python 示例 饮水记录

    因为每天都需要喝水  这是非常重要的 目录结构: ├─bin│ │ start.py│ ││ └─__pycache__│ start.cpython-36.pyc│├─core│ │ src.py│ ...

  3. js实现手机号身份证等加星(*)号

    下面来为各位整理了一些关于js实现手机号身份证等加星(*)号代码了,在js不足时我们还补充了php实现手机号身份证等加星(*)号的函数,有兴趣的一起来看看.   有时候为了不让用户的手机号码和身份证号 ...

  4. 【Linux_Unix系统编程】Chapter10 时间

    chapter10 时间 1:真实时间:度量这一时间的起点有二:(1)某个标准点:(2)进程生命周期内的某个固定时点(通常为程序启动) 2:进程时间:一个进程所使用的CPU时间总量,适用于对程序,算法 ...

  5. QEMU,KVM及QEMU-KVM介绍

    What's QEMU QEMU是一个主机上的VMM(virtual machine monitor),通过动态二进制转换来模拟CPU,并提供一系列的硬件模型,使guest os认为自己和硬件直接打交 ...

  6. Flask上下文管理源码分析

    上下文管理本质(类似于threading.local): 1.每一个线程都会在Local类中创建一条数据: { "唯一标识":{stark:[ctx,]}, "唯一标识& ...

  7. 并发基础(九) java线程的终止与中断

    1.简单了解一下:为何不赞成使用 Thread.stop.Thread.suspend 和 Thread.resume?   suspend .resume.stop方法分别完成了线程的暂停.恢复.终 ...

  8. shell中使用函数

    函数定义.调用 $ cat te.sh #!/bin/bash # define a function test() { echo "This is a function." } ...

  9. Static / Const 的概念

    C/C++/Java Static / Const 的概念 这里以C为准,其他语言类似. Static变量是指分配不变(只可分配一次,以后再分配就无效了.)的变量 -- 它的存活寿命或伸展域可以贯穿程 ...

  10. openstack 基镜像与差异镜像的整合