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.