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

传送门

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

题目大意

给定一个图,有一级道路和二级道路,要求一级道路数量不少于k,求其生成树中最大边最小的值

题解

很明显的二分答案
验证时,用Kruskal判断,先扫一遍一级数值小于答案加入,如果条数小于k返回不行,扫所有数值小于答案的,条数为n-1的返回可以
反之不行

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
 readln(n,t,m);
 for i:=1 to m-1 do
  begin
   readln(a,b,c,d);
   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.
    原文作者:道路修建问题
    原文地址: https://blog.csdn.net/slongle_amazing/article/details/49954121
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞