TZOJ 2754 Watering Hole(最小生成树Kruskal)
描述
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)的更多相关文章
- Kruskal || BZOJ 1601: [Usaco2008 Oct]灌水 || Luogu P1550 [USACO08OCT]打井Watering Hole
题面:P1550 [USACO08OCT]打井Watering Hole 题解:无 代码: #include<cstdio> #include<cstring> #includ ...
- p1221网络布线(最小生成树 Prim(普里母)算法) p1222 Watering Hole
描述 Description 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当然,他需要你的帮助.约翰已经给他的农场安排了一条高速的网络线路,他想把这条线路 ...
- [USACO08OCT]Watering Hole
[USACO08OCT]Watering Hole 题目大意: Farmer John 有\(n(n\le300)\)个牧场,他希望灌溉他的所有牧场.牧场编号为\(1\sim n\),要灌溉一个牧场有 ...
- Luogu P1550 打井Watering Hole
P1550 [USACO08OCT]打井Watering Hole 题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to ...
- bzoj1601 / P1550 [USACO08OCT]打井Watering Hole(堆优化prim)
P1550 [USACO08OCT]打井Watering Hole 对于自己建水库的情况,新建一个虚拟结点,和其他点的边权即为自建水库的费用 这样问题就转化为一个裸最小生成树问题了. 这里用堆优化 ...
- 模板——最小生成树kruskal算法+并查集数据结构
并查集:找祖先并更新,注意路径压缩,不然会时间复杂度巨大导致出错/超时 合并:(我的祖先是的你的祖先的父亲) 找父亲:(初始化祖先是自己的,自己就是祖先) 查询:(我们是不是同一祖先) 路径压缩:(每 ...
- 洛谷P1550 [USACO08OCT]打井Watering Hole
P1550 [USACO08OCT]打井Watering Hole 题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to ...
- 最小生成树——Kruskal与Prim算法
最小生成树——Kruskal与Prim算法 序: 首先: 啥是最小生成树??? 咳咳... 如图: 在一个有n个点的无向连通图中,选取n-1条边使得这个图变成一棵树.这就叫“生成树”.(如下图) 每个 ...
- 【转】最小生成树——Kruskal算法
[转]最小生成树--Kruskal算法 标签(空格分隔): 算法 本文是转载,原文在最小生成树-Prim算法和Kruskal算法,因为复试的时候只用到Kruskal算法即可,故这里不再涉及Prim算法 ...
随机推荐
- shell 10流程控制
if 判断 if #shell #!/bin/sh a=5 if [[ $a > 3 ]];then echo "$a>3" fi #写成一行 if [[ $a < ...
- IE, Firefox下,checkbox的钩钩一旦勾上,画面再刷新,钩钩还是勾上的解决方案
如题,IE, Firefox下,checkbox的钩钩一旦勾上,画面再刷新,钩钩还是勾上的解决方案 <input type="checkbox" />加上属性auto ...
- 1053 Path of Equal Weight (30 分)
Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weig ...
- javascript 节点操作拷贝节点cloneNode()
cloneNode(a)方法接受一个布尔值参数,表示是否深拷贝 true:表示执行深拷贝,复制本节点以及整个子节点树. false:浅拷贝.只复制节点本身. 复制后返回的节点副本属于文档所有,但是并没 ...
- StrokesPlus发送快捷键命令列表
StrokesPlus重度使用者! @ = Windows按键 + = SHIFT按键 ^ = CTRL按键 % = ALT按键 {ADD} = VK_ADD {APPS} = VK_APPS {AT ...
- ajaxGet 获取封装
callback 表示下一个功能(回调函数) function ajaxGet(url,callback,data){ 如果路径上有参数 就在url后面拼接参数 否则只请求url ...
- 记录——node-mysql连接池遇到的全局变量问题
记录一个折腾了快2个小时的BUG,目前还不清楚原理. 系统分别在阿里云(测试用).XL服务器上部署,此次BUG所在功能模块为生成表格并下载,表格数据由120(阿里云)上的数据库提供. 阿里云上一切正常 ...
- 委托学习过程及委托、Lambda表达式和匿名方法的关系总结及事件总结
第一章,当开始学习委托的时候,我们会问什么是委托?为什么要学习委托? 一,什么是委托? 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做法, ...
- aspx后台代码写在前段
合并cs的代码 <%@ Page Language="C#" AutoEventWireup="true" Inherits="System.W ...
- 过滤器 ;spring拦截器 切片 小结
1. springMVc的拦截器 实现HandlerInterceptor接口,如下: public class HandlerInterceptor1 implements HandlerInter ...