用的是克鲁斯卡尔算法,跟2485差不多,也是求最小生成树中最长的一段

program poj2395;
type
node=record
x,y,c:longint;
end;
var
a:array[1..10000] of node;
father:array[1..2000] of integer;
n,m,i,k,ans:longint;
procedure quick(left,right:integer);
var
i,j,temp:longint;
p:node;
begin
i:=left;
j:=right;
temp:=a[(i+j) div 2].c;
while i<=j do
begin
while a[i].c<temp do inc(i);
while a[j].c>temp do dec(j);
if i<=j then
begin
p:=a[i];
a[i]:=a[j];
a[j]:=p;
inc(i);
dec(j);
end;
end;
if left<j then quick(left,j);
if i<right then quick(i,right);
end;
function getfather(num:integer):integer;
begin
if father[num]=num then getfather:=num
else
begin
father[num]:=getfather(father[num]);
getfather:=father[num];
end;
end;
begin
fillchar(a,sizeof(a),0);
readln(n,m);
for i:=1 to m do
readln(a[i].x,a[i].y,a[i].c);
quick(1,m);
for i:=1 to n do
father[i]:=i;
ans:=-maxlongint;
k:=0;
for i:=1 to m do
if getfather(a[i].x)<>getfather(a[i].y) then
begin
father[getfather(a[i].y)]:=getfather(a[i].x);
if ans<a[i].c then ans:=a[i].c;
inc(k);
if k=n-1 then break;
end;
writeln(ans);
end.

优质内容筛选与推荐>>
1、Eclipse断点调试
2、字符串消除
3、mp3播放器(六)(播放功能实现)
4、linux shell 基本规范
5、3.IOC的配置与应用(annotation的方式)


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号