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的更多相关文章

  1. POJ 1797 Heavy Transportation (Dijkstra变形)

    F - Heavy Transportation Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & ...

  2. poj 1797 Heavy Transportation(最大生成树)

    poj 1797 Heavy Transportation Description Background Hugo Heavy is happy. After the breakdown of the ...

  3. POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径)

    POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径) Description Background Hugo ...

  4. POJ.1797 Heavy Transportation (Dijkstra变形)

    POJ.1797 Heavy Transportation (Dijkstra变形) 题意分析 给出n个点,m条边的城市网络,其中 x y d 代表由x到y(或由y到x)的公路所能承受的最大重量为d, ...

  5. POJ 1797 ——Heavy Transportation——————【最短路、Dijkstra、最短边最大化】

    Heavy Transportation Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64 ...

  6. Heavy Transportation POJ 1797 最短路变形

    Heavy Transportation POJ 1797 最短路变形 题意 原题链接 题意大体就是说在一个地图上,有n个城市,编号从1 2 3 ... n,m条路,每条路都有相应的承重能力,然后让你 ...

  7. POJ 1797 Heavy Transportation (最大生成树)

    题目链接:POJ 1797 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter pro ...

  8. POJ 1797 Heavy Transportation (Dijkstra)

    题目链接:POJ 1797 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter pro ...

  9. poj 1797(并查集)

    http://poj.org/problem?id=1797 题意:就是从第一个城市运货到第n个城市,最多可以一次运多少货. 输入的意思分别为从哪个城市到哪个城市,以及这条路最多可以运多少货物. 思路 ...

  10. Poj(1797) Dijkstra对松弛条件的变形

    题目链接:http://poj.org/problem?id=1797 题意:从路口1运货到路口n,最大的运货重量是多少?题目给出两路口间的最大载重. 思路:j加到s还是接到K下面,取两者的较大者,而 ...

随机推荐

  1. 3dContactPointAnnotationTool开发日志(十四)

      貌似每次让用户手动输入文件路径太不人道了,于是参考Unity 实用教程 之 调用系统窗口选择文件或路径增加了让用户浏览文件的功能,点击输入框旁边的+就可以找到文件并加载进来:   貌似调整位置再计 ...

  2. Qt-excel文件操作方法

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt-excel文件操作方法     本文地址:http://techieliang.com/ ...

  3. 【week3】词频统计 单元测试

    使用Eclipse 集成的Junit进行单元测试.单元测试的核心包括断言.注解. 测试代码如下: @BeforeClass // 针对所有测试,只执行一次,且必须为static void public ...

  4. 【Linux】- CentOS 防火墙iptables和firewall

    1 iptables防火墙 1.1 基本操作 # 查看防火墙状态 service iptables status   # 停止防火墙 service iptables stop   # 启动防火墙 s ...

  5. 抽象类 C#

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  6. c++的一些编程技巧和细节

    1.函数形参,如: CreateProcess(                  NULL,                  cmdbuf,                  NULL,      ...

  7. WebService部署服务器调试时提示 “测试窗体只能用于来自本地计算机的请求”解决方法

    原因:没有开启服务器访问权限! 解决方法: 在web.config的<system.web></system.web>中加入如下配置节内容即可解决 <webService ...

  8. codeforces 985 E. Pencils and Boxes (dp 树状数组)

    E. Pencils and Boxes time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  9. 使用for循环遍历数组元素

    循环可以将代码块执行指定的次数.如果您希望一遍又一遍地运行相同的代码,并且每次的值都不同,那么使用循环是很方便的.迭代语句又叫循环语句. JavaScript 支持不同类型的循环: for - 循环代 ...

  10. poj 1274 The Perfect Stall (二分匹配)

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17768   Accepted: 810 ...