bzoj 1601 最小生成树
原题传送门http://www.lydsy.com/JudgeOnline/problem.php?id=1601
最小生成树的比较水的题,我们只需要加一个源点,连向所有的点,边权为每个点建水库的代价
/**************************************************************
Problem:
User: BLADEVIL
Language: Pascal
Result: Accepted
Time: ms
Memory: kb
****************************************************************/ //By BLADEVIL
var
n :longint;
pre, other, len :array[..] of longint;
tot :longint;
father :array[..] of longint;
ans :longint; procedure swap(var a,b:longint);
var
c :longint;
begin
c:=a; a:=b; b:=c;
end; procedure qs(low,high:longint);
var
i, j, x :longint;
begin
i:=low; j:=high; x:=len[(i+j) div ];
while i<j do
begin
while len[i]<x do inc(i);
while len[j]>x do dec(j);
if i<=j then
begin
swap(len[i],len[j]);
swap(pre[i],pre[j]);
swap(other[i],other[j]);
inc(i); dec(j);
end;
end;
if i<high then qs(i,high);
if j>low then qs(low,j);
end; procedure init;
var
i, j :longint;
x :longint; begin
read(n);
for i:= to n do
begin
inc(tot);
pre[tot]:=n+;
other[tot]:=i;
read(len[tot]);
end;
for i:= to n do
for j:= to n do
if i<>j then
begin
inc(tot);
pre[tot]:=i;
other[tot]:=j;
read(len[tot]);
end else read(x);
qs(,tot);
end; function getfather(x:longint):longint;
begin
if father[x]=x then exit(x);
father[x]:=getfather(father[x]);
exit(father[x]);
end; procedure main;
var
i :longint;
x, y, fx, fy :longint; begin
for i:= to n+ do father[i]:=i;
for i:= to tot do
begin
x:=pre[i]; y:=other[i];
fx:=getfather(x);
fy:=getfather(y);
if fx<>fy then
begin
inc(ans,len[i]);
father[fx]:=fy;
end;
end;
writeln(ans);
end; begin
init;
main;
end.
bzoj 1601 最小生成树的更多相关文章
- BZOJ 1601 [Usaco2008 Oct]灌水:最小生成树
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1601 题意: Farmer John已经决定把水灌到他的n(1<=n<=300 ...
- BZOJ 1601 [Usaco2008 Oct]灌水 (最小生成树)
题意 Farmer John已经决定把水灌到他的n(1<=n<=300)块农田,农田被数字1到n标记.把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库. 建造一个水库需要 ...
- BZOJ 1601: [Usaco2008 Oct]灌水 最小生成树_超级源点
Description Farmer John已经决定把水灌到他的n(1<=n<=300)块农田,农田被数字1到n标记.把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库. ...
- bzoj 1601: [Usaco2008 Oct]灌水【最小生成树】
挺有意思的思路 如果不能自己打井,那么就是MST裸题了,考虑转换一下,自己打井就相当于连接一口虚拟的井(地下水?),所有井i到这口井的距离是w[i],这样把所有边排个序跑MST即可 #include& ...
- BZOJ 1601 [Usaco2008 Oct]灌水
1601: [Usaco2008 Oct]灌水 Time Limit: 5 Sec Memory Limit: 162 MB Description Farmer John已经决定把水灌到他的n(1 ...
- 【BZOJ 1601】 灌水
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1601 [算法] 最小生成树 [代码] #include<bits/stdc++ ...
- BZOJ 1016 最小生成树计数
Description 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的最小生成树.(如果两颗最小生成树中至少有一条边不同,则这两个最小生成树就是不同的 ...
- BZOJ 2521 最小生成树(最小割)
http://www.lydsy.com/JudgeOnline/problem.php?id=2521 题意:每次能增加一条边的权值1,求最小代价让一条边保证在最小生成树里 思路:如果两个点中有环, ...
- BZOJ 2561: 最小生成树(最小割)
U,V能在最小(大)生成树上,当且仅当权值比它小(大)的边无法连通U,V. 两次最小割就OK了. --------------------------------------------------- ...
随机推荐
- BI领军者之一Tableau试用浅谈
下图是最新的Gartner BI Magic Quadrant,其中领军者之一的Tableau表现的异常突出,执行力象限上直接甩开其它产品一条街,前瞻性象限上略微超越了MSBI,怀着无比的好奇心,特意 ...
- 失败的尝试,使用继承扩展数组,以及ES6的必要性
我们都知道直接在原生对象上扩展对象是很不好的.所以prototype这样的库广受非议. 一些库,比如lodash采用了工具包形式的扩展方式,绕开了对象的继承. 由于es6的class的出现,我尝试以A ...
- vue2.0 $emit $on组件通信
在vue1.0中父子组件通信使用$dispatch 和 $broadcast,但是在vue2.0中$dispatch 和 $broadcast 已经被弃用. 因为基于组件树结构的事件流方式实在是让人难 ...
- Python进程、线程、协程及IO多路复用
详情戳击下方链接 Python之进程.线程.协程 python之IO多路复用
- Selenium LoadableComponent加载组件
继承LoadableComponent类可以在打开地址时, 判断浏览器是否打开了预期的网址, 需要重写load()与isLoad()方法: 即使没有定义get()方法, 也可以进行get()方法的调用 ...
- P5056 插头dp
题面 Source: unordered_map: #include <iostream> #include <tr1/unordered_map> #include < ...
- LeetCode 81——搜索旋转排序数组 II
1. 题目 2. 解答 2.1. 方法一 基于 LeetCode 33--搜索旋转排序数组 中的方法二. 当 nums[mid] = nums[right] 时,比如 [1, 1, 2, 1, 1], ...
- tensorflow学习笔记(2)-反向传播
tensorflow学习笔记(2)-反向传播 反向传播是为了训练模型参数,在所有参数上使用梯度下降,让NN模型在的损失函数最小 损失函数:学过机器学习logistic回归都知道损失函数-就是预测值和真 ...
- 如何使用remix
---恢复内容开始--- 教程 | [Ethereum 智能合约开发笔记]使用 Remix Ajian | 3. Feb, 2018 | 621 次阅读 教程 Remix 开发工具 之前有自己开发过E ...
- linux备忘录-正则表达式与文件格式化处理
正则表达式 POSIX标准的符号 [:alnum:] -> 英文大小写字母和数字 0-9,A-Z,a-z [:alpha:] -> 英文大小写字母 A-Z,a-z [:blank:] -& ...