BZOJ 1601 USACO 2008 Oct. 灌水
【Description】
Farmer John已经决定把水灌到他的n(1<=n<=300)块农田,农田被数字1到n标记。把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库。 建造一个水库需要花费wi(1<=wi<=100000),连接两块土地需要花费Pij(1<=pij<=100000,pij=pji,pii=0). 计算Farmer John所需的最少代价。
【题解】
新建一个节点0,把各个点与0点连边,边权为在该点上建水库的代价
然后跑一边最小生成树就行了
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=,maxm=;
int n,fa[maxn],tot=,num=;
long long ans=;
struct edge{int x,y,w;}e[maxm];
void read(int &k){
k=; int f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
k*=f;
}
int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
bool cmp(edge a,edge b){return a.w<b.w;}
int main(){
read(n); for (int i=;i<=n;i++) fa[i]=i;
for (int i=;i<=n;i++)e[++tot].x=i,e[tot].y=,read(e[tot].w);
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)e[++tot].x=i,e[tot].y=j,read(e[tot].w);
sort(e+,e++tot,cmp);
for (int i,x,y=;i<=tot;i++){
if (find(x=e[i].x)!=find(y=e[i].y)){
ans+=e[i].w; fa[find(x)]=find(y); num++;
}
if (num==n) break;
}
return printf("%lld\n",ans),;
}
BZOJ 1601 USACO 2008 Oct. 灌水的更多相关文章
- BZOJ 1602 USACO 2008 Oct. 牧场行走
[题解] 要求出树上两点间的距离,树上的边有边权,本来应该是个LCA. 看他数据小,Xjb水过去了...其实也算是LCA吧,一个O(n)的LCA... #include<cstdio> # ...
- BZOJ 1603 USACO 2008 Oct. 打谷机
[题解] 水题.. 保存连接方式,按顺序处理即可. #include<cstdio> #include<algorithm> using namespace std; int ...
- 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 ...
- bzoj1601: [Usaco2008 Oct]灌水
经典延伸最小生成树问题... #include<cstdio> #include<cstring> #include<cctype> #include<alg ...
- [BZOJ1601][Usaco2008 Oct]灌水 最小生成树水题
1601: [Usaco2008 Oct]灌水 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 2121 Solved: 1393[Submit][St ...
- 【BZOJ】1601: [Usaco2008 Oct]灌水(kruskal)
http://www.lydsy.com/JudgeOnline/problem.php?id=1601 很水的题,但是一开始我看成最短路了T_T 果断错. 我们想,要求连通,对,连通!连通的价值最小 ...
- BZOJ 1601 [Usaco2008 Oct]灌水:最小生成树
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1601 题意: Farmer John已经决定把水灌到他的n(1<=n<=300 ...
- BZOJ——1601: [Usaco2008 Oct]灌水
http://www.lydsy.com/JudgeOnline/problem.php?id=1601 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: ...
随机推荐
- 【剑指offer】斐波那契序列与跳台阶
转载请注明出处:http://blog.csdn.net/ns_code/article/details/25337983 剑指offer上的第9题,简单题,在九度OJ上測试通过. 主要注意下面几点: ...
- oc76--NSMutableDictionary
// // main.m // NSMutableDictionary // NSDictionary不可变,初始化后就不可以修改,NSMutableDictionary可变,初始化后可以改变. // ...
- Linux ALSA声卡驱动之四:Control设备的创建
声明:本博内容均由http://blog.csdn.net/droidphone原创,转载请注明出处,谢谢! Control接口 Control接口主要让用户空间的应用程序(alsa-lib)可以访问 ...
- linux设备驱动模型二【转】
本文转载自:http://blog.csdn.net/u013904227/article/details/51167886 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] ...
- mst
https://www.zybuluo.com/ysner/note/1245941 题面 给一个\(n\)点完全图,点权均小于\(2^m\).定义边权等于两端点点权的与和(即\(a_i\&b ...
- Vue导航守卫beforeRouteEnter,beforeRouteUpdate,beforeRouteLeave详解
Vue导航守卫以我自己的理解就是监听页面进入,修改,和离开的功能.每个守卫接受三个参数 to: Route: 即将要进入的目标路由对象 from: Route: 当前导航正要离开的路由 next: F ...
- CentOS 7.0 firewall防火墙关闭firewall作为防火墙,这里改为iptables防火墙
CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙步骤: 1.先检查是否安装了: iptables service iptables status 2.安装ip ...
- 【Codeforces866E_CF866E】Hex Dyslexia(Structure & DP)
It's my first time to write a blog in EnglishChinglish, so it may be full of mistakes in grammar. Pr ...
- 327 Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- C# 定时无操作则退出登陆,回到登陆界面。
有时候根据需求需要为程序添加在规定的时间内无操作则退出当前的登陆程序的功能,如下代码模拟描述的需求功能. using System; using System.Collections.Generic; ...