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


    1196: [HNOI2006]公路修建问题

    Time Limit: 10 Sec  Memory Limit: 162 MB
    Submit: 1107  Solved: 583
    [Submit][Status]

    Description

    OI island是一个非常漂亮的岛屿,自开发以来,到这儿来旅游的人很多。然而,由于该岛屿刚刚开发不久,所以那里的交通情况还是很糟糕。所以,OIER Association组织成立了,旨在建立OI island的交通系统。 OI island有n个旅游景点,不妨将它们从1到n标号。现在,OIER Association需要修公路将这些景点连接起来。一条公路连接两个景点。公路有,不妨称它们为一级公路和二级公路。一级公路上的车速快,但是修路的花费要大一些。 OIER Association打算修n-1条公路将这些景点连接起来(使得任意两个景点之间都会有一条路径)。为了保证公路系统的效率, OIER Association希望在这n-1条公路之中,至少有k条(0≤k≤n-1)一级公路。OIER Association也不希望为一条公路花费的钱。所以,他们希望在满足上述条件的情况下,花费最多的一条公路的花费尽可能的少。而你的任务就是,在给定一些可能修建的公路的情况下,选择n-1条公路,满足上面的条件。

    Input

    第一行有三个数n(1≤n≤10000),k(0≤k≤n-1),m(n-1≤m≤20000),这些数之间用空格分开。 N和k如前所述,m表示有m对景点之间可以修公路。以下的m-1行,每一行有4个正整数a,b,c1,c2 (1≤a,b≤n,a≠b,1≤c2≤c1≤30000)表示在景点a与b 之间可以修公路,如果修一级公路,则需要c1的花费,如果修二级公路,则需要c2的花费。

    Output

    一个数据,表示花费最大的公路的花费。

    Sample Input

    10 4 20
    3 9 6 3
    1 3 4 1
    5 3 10 2
    8 9 8 7
    6 8 8 3
    7 1 3 2
    4 9 9 5
    10 8 9 1
    2 6 9 1
    6 7 9 8
    2 6 2 1
    3 8 9 5
    3 2 9 6
    1 6 10 3
    5 6 3 1
    2 7 6 1
    7 8 6 2
    10 9 2 1
    7 1 10 2

    Sample Output

    5

    HINT

     

    Source

    题解:

    这种最大值最小的一般用二分。

    判断是否可行我们可以利用并查集看是否能选出n-1条边

    如何选呢?我们可以按c1值排序,从前往后扫

    如果该边c1<=mid 或者 c1>mid and c2<=mid and tot>=k 也就是说如果可建一级公路就建,否则不能建一级公路,但可能能建二级公路,

    但建二级公路的前提是已经建够了k条一级公路,否则后面c1值越来越大,更不可能建够k条一级公路

    代码:

     1 const maxm=20000+1000;
     2 var  a,b,c,d,fa:array[0..maxm] of longint;
     3      i,n,m,k,l,r,mid:longint;
     4 procedure swap(var x,y:longint);
     5  var t:longint;
     6    begin
     7    t:=x;x:=y;y:=t;
     8    end;
     9 procedure sort(l,r:longint);
    10  var i,j,x,y:longint;
    11  begin
    12  i:=l;j:=r;x:=c[(i+j)>>1];
    13  repeat
    14   while c[i]<x do inc(i);
    15   while c[j]>x do dec(j);
    16   if i<=j then
    17    begin
    18    swap(a[i],a[j]);swap(b[i],b[j]);
    19    swap(c[i],c[j]);swap(d[i],d[j]);
    20    inc(i);dec(j);
    21    end;
    22  until i>j;
    23  if i<r then sort(i,r);
    24  if j>l then sort(l,j);
    25  end;
    26 procedure init;
    27  begin
    28    readln(n,k,m);dec(m);
    29    for i:=1 to m do readln(a[i],b[i],c[i],d[i]);
    30  end;
    31 function find(x:longint):longint;
    32  begin
    33  if fa[x]<>x then fa[x]:=find(fa[x]);
    34  exit(fa[x]);
    35  end;
    36 
    37 function test(x:longint):boolean;
    38  var i,j,cnt,tot:longint;
    39  begin
    40  for i:=1 to n do fa[i]:=i;cnt:=0;tot:=0;
    41  j:=1;
    42  while (j<=m) do
    43    begin
    44      while (find(a[j])=find(b[j])) and (j<m) do inc(j);
    45      if find(a[j])=find(b[j]) then break;
    46      if (c[j]<=x) or ((c[j]>x) and (d[j]<=x) and (tot>=k)) then
    47        begin
    48         inc(cnt);fa[find(a[j])]:=find(b[j]);
    49         if c[j]<=x then inc(tot);
    50        end;
    51      inc(j);
    52    end;
    53  exit(cnt=n-1);
    54  end;
    55 procedure main;
    56  begin
    57    sort(1,m);
    58    l:=0;r:=30000;
    59    while l<r do
    60     begin
    61       mid:=(l+r)>>1;
    62       if test(mid) then r:=mid else l:=mid+1;
    63     end;
    64    writeln(l);
    65  end;
    66 
    67 begin
    68   assign(input,'input.txt');assign(output,'output.txt');
    69   reset(input);rewrite(output);
    70   init;
    71   main;
    72   close(input);close(output);
    73 end.    
    View Code
  • 相关阅读:
    总有段迷惑的人生
    codepage的重要性【转】
    开通博客
    js 正则常用方法
    关于小程序scrollview组件添加enableflex后布局失效的解决方案
    关于小程序使用async/await报错regeneratorRuntime is not defined的解决方案
    IE6中,一个Button同时打开两个下载窗口,并且可以自动关闭
    Create User
    Oracle: import tables use .dmp file in PL/SQL Developer
    VS在进行调试时,不能调试的原因列举如下
  • 原文地址:https://www.cnblogs.com/zyfzyf/p/3916655.html
Copyright © 2020-2023  润新知