poj 1797
2013-09-08 09:48
最大生成树,输出生成树中最短的边儿即可
或者对边儿排序,二份答案+BFS判断是否1连通N
时间复杂度都是O(NlogN)的
附最大生成树pascal代码
//By BLADEVIL
var
m, n, t :longint;
pre, other, len :array[0..101000] of longint;
father :array[0..100100] of longint;
i :longint; procedure init;
var
i :longint;
x, y, z :longint;
begin
read(n,m);
for i:=1 to m do
begin
read(x,y,z);
pre[i]:=x;
other[i]:=y;
len[i]:=z;
end;
for i:=1 to n do father[i]:=i;
end; procedure qs(low,high:longint);
var
i, j, x, z :longint;
begin
i:=low; j:=high; x:=len[(i+j) div 2];
while i<j do
begin
while x>len[j] do dec(j);
while x<len[i] do inc(i);
if i<=j then
begin
z:=len[i]; len[i]:=len[j]; len[j]:=z;
z:=pre[i]; pre[i]:=pre[j]; pre[j]:=z;
z:=other[i]; other[i]:=other[j]; other[j]:=z;
inc(i); dec(j);
end;
end;
if i<high then qs(i,high);
if j>low then qs(low,j);
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
j :longint;
a, b :longint;
begin
init;
qs(1,m);
for j:=1 to m do
begin
a:=getfather(pre[j]);
b:=getfather(other[j]);
if a<>b then father[b]:=a;
a:=getfather(1);
b:=getfather(n);
if a=b then
begin
writeln('Scenario #',i,':');
writeln(len[j]);
writeln;
exit;
end;
end;
end; begin
read(t);
for i:=1 to t do main;
end.
poj 1797的更多相关文章
- POJ 1797 Heavy Transportation (Dijkstra变形)
F - Heavy Transportation Time Limit:3000MS Memory Limit:30000KB 64bit IO Format:%I64d & ...
- poj 1797 Heavy Transportation(最大生成树)
poj 1797 Heavy Transportation Description Background Hugo Heavy is happy. After the breakdown of the ...
- POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径)
POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径) Description Background Hugo ...
- POJ.1797 Heavy Transportation (Dijkstra变形)
POJ.1797 Heavy Transportation (Dijkstra变形) 题意分析 给出n个点,m条边的城市网络,其中 x y d 代表由x到y(或由y到x)的公路所能承受的最大重量为d, ...
- POJ 1797 ——Heavy Transportation——————【最短路、Dijkstra、最短边最大化】
Heavy Transportation Time Limit:3000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64 ...
- Heavy Transportation POJ 1797 最短路变形
Heavy Transportation POJ 1797 最短路变形 题意 原题链接 题意大体就是说在一个地图上,有n个城市,编号从1 2 3 ... n,m条路,每条路都有相应的承重能力,然后让你 ...
- POJ 1797 Heavy Transportation (最大生成树)
题目链接:POJ 1797 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter pro ...
- POJ 1797 Heavy Transportation (Dijkstra)
题目链接:POJ 1797 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter pro ...
- poj 1797(并查集)
http://poj.org/problem?id=1797 题意:就是从第一个城市运货到第n个城市,最多可以一次运多少货. 输入的意思分别为从哪个城市到哪个城市,以及这条路最多可以运多少货物. 思路 ...
- Poj(1797) Dijkstra对松弛条件的变形
题目链接:http://poj.org/problem?id=1797 题意:从路口1运货到路口n,最大的运货重量是多少?题目给出两路口间的最大载重. 思路:j加到s还是接到K下面,取两者的较大者,而 ...
随机推荐
- matlab的二维卷积操作(转)
MATLAB的conv2函数实现步骤(conv2(A,B)): 其中,矩阵A和B的尺寸分别为ma*na即mb*nb ① 对矩阵A补零,第一行之前和最后一行之后都补mb-1行,第一列之前和最后一列之后都 ...
- C#窗口文件双击打开时出错
出错原因: 1. 修改了该窗口文件的.Designer.cs文件中:#region Windows 窗体设计器生成的代码这里面的代码,导致运行不正常. 为了传递数据,我在构造函数中增加了传递的值. 需 ...
- web传参
页面通过对象,将表单数据传送给后端,后端通过对象接收参数值,
- Tomcat启动报错ERROR:transport error 202:bind failed:Address already
昨天在服务器上拷贝了一个tomcat项目,修改了server.xml之后启动居然报错ERROR:transport error 202:bind failed:Address already,应该是远 ...
- MySQL & export
MySQL & export mysql export table form command line https://cn.bing.com/search?q=mysql%20export% ...
- [OS] 多线程--第一次亲密接触CreateThread与_beginthreadex本质区别
转自:http://blog.csdn.net/morewindows/article/details/7421759 本文将带领你与多线程作第一次亲密接触,并深入分析CreateThread与_be ...
- jquery animate() stop() finish() 方法使用
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- matlab edge
edge在matlab中用来进行边缘检测BW = edge(I) 采用灰度或一个二值化图像I作为它的输入,并返回一个与I相同大小的二值化图像BW,在函数检测到边缘的地方为1,其他地方为0. BW = ...
- hdu 1392 Surround the Trees (凸包)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- [2018国家集训队][UOJ449] 喂鸽子 [dp+组合数学]
题面 传送门 思路 首先,这道题是可以暴力min-max反演+NTT做出来的......但是这个不美观,我来讲一个做起来舒服一点的做法 一个非常basic的idea:我们发现在一只鸽子吃饱以后再喂给它 ...