# [BZOJ1196] [HNOI2006]公路修建问题

### 传送门

http://www.lydsy.com/JudgeOnline/problem.php?id=1196

### 题解

var
x:array[0..50000,1..4]of longint;
sum,fa:array[0..10005]of longint;
i,j,k:longint;
n,m,t:longint;
a,b,c,d:longint;
ans,l,r,mid,summ:longint;
procedure sort(l,r:longint);
var i,j,a,b:longint;
begin
i:=l; j:=r; a:=x[(l+r) div 2,3];
repeat
while x[i,3]<a do inc(i);
while a<x[j,3] do dec(j);
if not(i>j) then
begin
b:=x[i,1]; x[i,1]:=x[j,1]; x[j,1]:=b;
b:=x[i,2]; x[i,2]:=x[j,2]; x[j,2]:=b;
b:=x[i,3]; x[i,3]:=x[j,3]; x[j,3]:=b;
b:=x[i,4]; x[i,4]:=x[j,4]; x[j,4]:=b;
inc(i); dec(j);
end;
until i>j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end;

function get(a:longint):longint;
begin
if fa[a]=a then exit(a);
fa[a]:=get(fa[a]);
exit(fa[a]);
end;

function check(ans:longint):longint;
begin
summ:=0;
for i:=1 to n do
fa[i]:=i;
for i:=2*m-2 downto 1 do
begin
if x[i,3]>ans then continue;
if x[i,4]=0 then continue;
a:=get(x[i,1]); b:=get(x[i,2]);
if a<>b then begin inc(summ); fa[a]:=b; end;
end;
if summ<t then exit(0);
for i:=1 to 2*m-2 do
begin
if x[i,3]>ans then continue;
a:=get(x[i,1]); b:=get(x[i,2]);
if a<>b then begin inc(summ); fa[a]:=b; end;
end;
if summ=n-1 then exit(1) else exit(0);
end;

begin
for i:=1 to m-1 do
begin
x[i*2-1,1]:=a; x[i*2-1,2]:=b; x[i*2-1,3]:=c; x[i*2-1,4]:=1;
x[i*2,1]:=a; x[i*2,2]:=b; x[i*2,3]:=d; x[i*2,4]:=0;
end;
sort(1,2*m-2); {x[i,3]}
l:=1; r:=x[2*m-2,3];
while l<r do
begin
mid:=(l+r)>>1;
if check(mid)=1
then r:=mid
else l:=mid+1;
end;
writeln(l);
end.

06-14 542

05-24 365

04-04 488

03-20 84

09-26 254

08-17 369

06-13 221

12-26 67

03-15 15

06-05 28