bzoj1601: [Usaco2008 Oct]灌水
经典延伸最小生成树问题。。。
#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
int read(){
int x=0;char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) x=x*10+c-'0',c=getchar();
return x;
}
const int nmax=305;
const int maxn=100005;
const int inf=0x7f7f7f7f;
struct edge{
int from,to,dist;
bool operator<(const edge &rhs)const{
return dist<rhs.dist;}
};
edge es[maxn];
int fa[nmax];
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
int main(){
int n=read(),u,v,d,cnt=0;
rep(i,1,n) {
es[++cnt].from=i,es[cnt].to=n+1,es[cnt].dist=read();
}
rep(i,1,n) rep(j,1,n){
u=read();
if(i!=j) es[++cnt].from=i,es[cnt].to=j,es[cnt].dist=u;
}
sort(es+1,es+cnt+1);
int ans=0,tot=0;
rep(i,1,n+1) fa[i]=i;
rep(i,1,cnt){
int ta=find(es[i].from),tb=find(es[i].to);
if(ta!=tb){
fa[ta]=tb;ans+=es[i].dist;
if(++tot==n) break;
}
}
printf("%d\n",ans);
return 0;
}
1601: [Usaco2008 Oct]灌水
Time Limit: 5 Sec Memory Limit: 162 MB
Submit: 1776 Solved: 1162
[Submit][Status][Discuss]
Description
Farmer John已经决定把水灌到他的n(1<=n<=300)块农田,农田被数字1到n标记。把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库。 建造一个水库需要花费wi(1<=wi<=100000),连接两块土地需要花费Pij(1<=pij<=100000,pij=pji,pii=0). 计算Farmer John所需的最少代价。
Input
*第一行:一个数n
*第二行到第n+1行:第i+1行含有一个数wi
*第n+2行到第2n+1行:第n+1+i行有n个被空格分开的数,第j个数代表pij。
Output
*第一行:一个单独的数代表最小代价.
Sample Input
5
4
4
3
0 2 2 2
2 0 3 3
2 3 0 4
2 3 4 0
Sample Output
输出详解:
Farmer John在第四块土地上建立水库,然后把其他的都连向那一个,这样就要花费3+2+2+2=9
HINT
Source
bzoj1601: [Usaco2008 Oct]灌水的更多相关文章
- [BZOJ1601][Usaco2008 Oct]灌水 最小生成树水题
1601: [Usaco2008 Oct]灌水 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 2121 Solved: 1393[Submit][St ...
- [BZOJ1601] [Usaco2008 Oct] 灌水 (kruskal)
Description Farmer John已经决定把水灌到他的n(1<=n<=300)块农田,农田被数字1到n标记.把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库. ...
- 【最小生成树】Bzoj1601[Usaco2008 Oct]灌水
Description Farmer John已经决定把水灌到他的n(1<=n<=300)块农田,农田被数字1到n标记.把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库. ...
- BZOJ1601 [Usaco2008 Oct]灌水[最小生成树]
显然分析可知这个图最后连起来是一个森林,每棵树有一个根再算一个代价.那么这些跟需要连向某一点一个建立水库的代价,且根可以有多个但不能没有,则考虑用超级源点0向所有点连虚边,Prim跑MST即可保证有至 ...
- BZOJ 1601 [Usaco2008 Oct]灌水
1601: [Usaco2008 Oct]灌水 Time Limit: 5 Sec Memory Limit: 162 MB Description Farmer John已经决定把水灌到他的n(1 ...
- 1601: [Usaco2008 Oct]灌水
1601: [Usaco2008 Oct]灌水 Time Limit: 5 Sec Memory Limit: 162 MB Submit: 1342 Solved: 881 [Submit][S ...
- 【BZOJ】1601: [Usaco2008 Oct]灌水(kruskal)
http://www.lydsy.com/JudgeOnline/problem.php?id=1601 很水的题,但是一开始我看成最短路了T_T 果断错. 我们想,要求连通,对,连通!连通的价值最小 ...
- BZOJ 1601 [Usaco2008 Oct]灌水 (最小生成树)
题意 Farmer John已经决定把水灌到他的n(1<=n<=300)块农田,农田被数字1到n标记.把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库. 建造一个水库需要 ...
- BZOJ 1601 [Usaco2008 Oct]灌水:最小生成树
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1601 题意: Farmer John已经决定把水灌到他的n(1<=n<=300 ...
随机推荐
- [C#]Linq To Xml 实例操作- 转
http://blog.sina.com.cn/s/blog_6c762bb301010oi5.html http://blog.xuite.net/cppbuilder/blog/9940157 在 ...
- ETL Pentaho Data Integration (Kettle) 插入/更新 问题 etl
Pentaho Data Integration (Kettle) 使用此工具 按 索引 做 插入更新操作时,也可能报 索引重复 的错误, 解决方法: 匹配的索引字段可能有null值,会导致此错误 ...
- linux下MySQL 5.6源码安装
linux下MySQL 5.6源码安装 1.下载:当前mysql版本到了5.6.20 http://dev.mysql.com/downloads/mysql 选择Source Code 2.必要软件 ...
- iPhone 6 & iPhone 6 Plus适配
转载请注明出处: http://www.cnblogs.com/dokaygang128/p/4049461.html Apple 今年发布了两款新的iPhone机器,iPhone 6 和iPhone ...
- EXT--表单AJax提交后台,返回前端数据格式的转换
前言: 前端发送请求至服务端(Java),得到的数据是Java语言对象所表现的形式,经常需要转换为JSON格式的字符串写出至前端:当前端获取后也往往需要将字符串转换为js的对象.本文描述了在EXT作为 ...
- 剑指offer--面试题8
题目:求旋转数组中的最小数字 以下为自己所写代码: #include "stdafx.h" #include <iostream> #include <excep ...
- 从String类看写C++ class需要注意的地方
#include <iostream> #include <string.h> using namespace std; class String { char* m_data ...
- Openstack Quantum project 更名为 Neuron
因为与磁带备份厂商Quantum商标冲突: The OpenStack Foundation has changed the name of its networking project from Q ...
- 【面试题003】c数组做为参数退化的问题,二维数组中的查找
[面试题003]c数组做为参数退化的问题,二维数组中的查找 一,c数组做为参数退化的问题 1.c/c++没有记录数组的大小,因此用指针访问数组中的元素的时候,我们要确保没有超过数组的边界, 通过下面 ...
- ASP .net(照片列表详细功能演示)
大家好,今天我们需要讲解的内容就是把上篇文章当中提到的照片列表的很多功能细化去做. 那么之间我们两篇文章的目的就是要让大家深刻体会get,post的使用场景极其作用.像一般处理程序的使用,隐藏域的使用 ...