原题传送门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 最小生成树的更多相关文章

  1. BZOJ 1601 [Usaco2008 Oct]灌水:最小生成树

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1601 题意: Farmer John已经决定把水灌到他的n(1<=n<=300 ...

  2. BZOJ 1601 [Usaco2008 Oct]灌水 (最小生成树)

    题意 Farmer John已经决定把水灌到他的n(1<=n<=300)块农田,农田被数字1到n标记.把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库. 建造一个水库需要 ...

  3. BZOJ 1601: [Usaco2008 Oct]灌水 最小生成树_超级源点

    Description Farmer John已经决定把水灌到他的n(1<=n<=300)块农田,农田被数字1到n标记.把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库. ...

  4. bzoj 1601: [Usaco2008 Oct]灌水【最小生成树】

    挺有意思的思路 如果不能自己打井,那么就是MST裸题了,考虑转换一下,自己打井就相当于连接一口虚拟的井(地下水?),所有井i到这口井的距离是w[i],这样把所有边排个序跑MST即可 #include& ...

  5. BZOJ 1601 [Usaco2008 Oct]灌水

    1601: [Usaco2008 Oct]灌水 Time Limit: 5 Sec  Memory Limit: 162 MB Description Farmer John已经决定把水灌到他的n(1 ...

  6. 【BZOJ 1601】 灌水

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1601 [算法] 最小生成树 [代码] #include<bits/stdc++ ...

  7. BZOJ 1016 最小生成树计数

    Description 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的最小生成树.(如果两颗最小生成树中至少有一条边不同,则这两个最小生成树就是不同的 ...

  8. BZOJ 2521 最小生成树(最小割)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2521 题意:每次能增加一条边的权值1,求最小代价让一条边保证在最小生成树里 思路:如果两个点中有环, ...

  9. BZOJ 2561: 最小生成树(最小割)

    U,V能在最小(大)生成树上,当且仅当权值比它小(大)的边无法连通U,V. 两次最小割就OK了. --------------------------------------------------- ...

随机推荐

  1. jmeter插件之VariablesFromCSV

    项目需求: 由于该项目的特殊性,需要新建很多个jmx脚本,并且这些jmx的全局用户变量都一样,如果要修改的话,没法做到统一修改. 实现思路: 为了满足需求,在jemter官网找到该插件Variable ...

  2. 关于ArrayList add()方法 中的引用问题

    ArrayList的add方法每次添加一个对象时,添加 的是一个对象的引用,比如进行循环操作10次  lists.add(a) 每次 a会改变 ,这时候你会发现你在lists里添加了10个相同的对象a ...

  3. Spring常用注解用法总结

    转自http://www.cnblogs.com/leskang/p/5445698.html 1.@Controller 在SpringMVC 中,控制器Controller 负责处理由Dispat ...

  4. Selenium 启动无头浏览器,只有chrome 和 firefox的,没有IE

    使用无头浏览器,可以避免掉不确定的弹出框对脚本稳定性的影响,还能在脚本执行过程中省略掉了css 加载的时间. 以下是Selenium 启动无头浏览器的方法,只有chrome 和 firefox的. p ...

  5. 阿里云SLB上http强制跳转到https问题处理

    背景: 最近一客户有一个需求,需要将外网所有http访问请求强制跳转到https,公网出口使用阿里云SLB,证书放在SLB上,SLB后端实例为ECS(webserver)web服务使用nginx, 网 ...

  6. 稳定匹配 - Stable Matching

    这篇文章将会对稳定匹配算法进行介绍及Python代码的实现,第一部分会针对稳定匹配的Gale-Shapley算法进行解析,第二部分就是用Python对该算法进行实现. 一.稳定匹配算法原理 1.1 介 ...

  7. 预研报告——MyBatis持久层的demo

    一.预研任务介绍和预研目标 任务介绍: 与 Hibernate 相比, MyBatis 是一个半自动化的持久层框架,以轻量级.效率高.原生代而好评如潮.虽然有在分享会上大致讲解,但是还是重新梳理成文字 ...

  8. [leetcode-635-Design Log Storage System]

    You are given several logs that each log contains a unique id and timestamp. Timestamp is a string t ...

  9. 【iOS开发】字典的快速赋值 setValuesForKeysWithDictionary

    前言 在学习解析数据的时候,我们经常是这么写的:PersonModel.h文件中 @property (nonatomic,copy)NSString *name; @property (nonato ...

  10. Python + OpenCV 实现LBP特征提取

    背景 看了些许的纹理特征提取的paper,想自己实现其中部分算法,看看特征提取之后的效果是怎样 运行环境 Mac OS Python3.0 Anaconda3(集成了很多包,浏览器界面编程,清爽) 步 ...