我们先二分一个答案,对于每个答案,先加一级公路,如果不够k直接break,

然后再加二级公路,加的过程类似Kruskal。

/**************************************************************
    Problem:
    User: BLADEVIL
    Language: Pascal
    Result: Accepted
    Time: ms
    Memory: kb
****************************************************************/
 
//By BLADEVIL
var
    n, m, k                 :longint;
    pre1, succ1, c1, c2     :array[..] of longint;
    pre2, succ2             :array[..] of longint;
    father                  :array[..] of longint;
    ans                     :longint;
     
procedure swap(var a,b:longint);
var
    c                       :longint;
begin
    c:=a; a:=b; b:=c;
end;
     
procedure qs1(low,high:longint);
var
    i, j, xx                :longint;
begin
    i:=low; j:=high;
    xx:=c1[(i+j) div ];
    while i<j do
    begin
        while c1[i]<xx do inc(i);
        while c1[j]>xx do dec(j);
        if i<=j then
        begin
            swap(pre1[i],pre1[j]);
            swap(succ1[i],succ1[j]);
            swap(c1[i],c1[j]);
            inc(i); dec(j);
        end;
    end;
    if i<high then qs1(i,high);
    if j>low then qs1(low,j);
end;
 
procedure qs2(low,high:longint);
var
    i, j, xx                :longint;
begin
    i:=low; j:=high;
    xx:=c2[(i+j) div ];
    while i<j do
    begin
        while c2[i]<xx do inc(i);
        while c2[j]>xx do dec(j);
        if i<=j then
        begin
            swap(pre2[i],pre2[j]);
            swap(succ2[i],succ2[j]);
            swap(c2[i],c2[j]);
            inc(i); dec(j);
        end;
    end;
    if i<high then qs2(i,high);
    if j>low then qs2(low,j);
end;
 
procedure init;
var
    i                       :longint;
     
begin
    read(n,k,m);
    for i:= to m- do
    begin
        read(pre1[i],succ1[i],c1[i],c2[i]);
        pre2[i]:=pre1[i]; succ2[i]:=succ1[i];
    end;
    qs1(,m-);
    qs2(,m-);
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
    l, r, mid               :longint;
    i                       :longint;
    fa, fb, a, b            :longint;
    cnt                     :longint;
     
begin
    l:=; r:=;
    while l<=r do
    begin
        mid:=(l+r) div ;
        cnt:=;
        for i:= to n do father[i]:=i;
        for i:= to m- do
        begin
            if c1[i]>mid then break;
            a:=pre1[i]; b:=succ1[i];
            fa:=getfather(a); fb:=getfather(b);
            if fa<>fb then
            begin
                father[fa]:=fb;
                inc(cnt);
            end;
        end;
        for i:= to m- do
        begin
            if (cnt<k) or (c2[i]>mid) then break;
            a:=pre2[i]; b:=succ2[i];
            fa:=getfather(a); fb:=getfather(b);
            if fa<>fb then
            begin
                father[fa]:=fb;
                inc(cnt);
            end;
        end;
        if cnt<n- then l:=mid+ else
        begin
            ans:=mid;
            r:=mid-;
        end;
    end;
    writeln(ans);
end;
 
 
begin
    init;
    main;
end.

bzoj 1196 二分+生成树判定的更多相关文章

  1. BZOJ 1196 二分答案+并查集

    http://www.lydsy.com/JudgeOnline/problem.php?id=1196 题目大意:n个城市,m-1条路,每条路有一级公路和二级公路之分,你要造n-1条路,一级公路至少 ...

  2. BZOJ 1196 二分+Kruskal

    思路: 二分答案 判一下能不能加 //By SirisuRen #include <cstdio> #include <cstring> #include <algori ...

  3. bzoj 1196

    http://www.lydsy.com/JudgeOnline/problem.php?id=1196 二分+并查集 一共有2*M条路径,我们首先将这2*M条路径按费用排序. 然后二分最大费用的公路 ...

  4. bzoj 1196 公路修建问题

    bzoj 1196: [HNOI2006]公路修建问题 Description OI island是一个非常漂亮的岛屿,自开发以来,到这儿来旅游的人很多.然而,由于该岛屿刚刚开发不久,所以那里的交通情 ...

  5. [NOIP 2010] 关押罪犯 (二分+二分图判定 || 并查集)

    题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...

  6. BZOJ 1196 [HNOI2006]公路修建问题:二分 + 贪心生成树check(类似kruskal)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1196 题意: n个城市,m对城市之间可以修公路. 公路有两种,一级公路和二级公路,在第i对 ...

  7. bzoj 1189 二分+最大流判定

    首先我们可以二分一个答案时间T,这样就将最优性问题 转化为了判定性问题.下面我们考虑对于已知的T的判定 对于矩阵中所有的空点bfs一次,得出来每个点到门的距离, 然后连接空点和每个能在t时间内到达的门 ...

  8. BZOJ 1196 [HNOI2006]公路修建问题(二分答案+并查集)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1196 [题目大意] 对于每条可能维修的公路可选择修一级公路或者二级公路,价值不同 要求 ...

  9. bzoj 1196: [HNOI2006]公路修建问题 二分+并查集

    题目链接 1196: [HNOI2006]公路修建问题 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1576  Solved: 909[Submit ...

随机推荐

  1. partial class 说明

    C# 2.0 可以将类.结构或接口的定义拆分到两个或多个源文件中,在类声明前添加partial关键字即可. 例如:下面的PartialTest类 class PartialTest { string ...

  2. Hive中典型的表内数据除重写法

    insert overwrite table store select t.p_key,t.sort_word from ( select p_key, sort_word , row_number( ...

  3. excute和query

    query(update goods set is_delete=1 where goods_id=13)总是出错??为什么, excute(update goods set is_delete=1 ...

  4. apache日志文件详解和实用分析命令

    apache日志文件每条数据的请意义,以及一些实用日志分析命令. 一.日志分析  如果apache的安装时采用默认的配置,那么在/logs目录下就会生成两个文件,分别是access_log和error ...

  5. mysql 导入导出数据库、数据表

    Linux下 均在控制台下操作. 导入数据库: 前提:数据库和数据表要存在(已经被创建) (1)将数据表 test_user.sql 导入到test 数据库的test_user 表中 [root@te ...

  6. 支付宝收款连接 非API

    <a href="https://shenghuo.alipay.com/send/payment/fill.htm?_form_token=mMYOrAXfReOtBBCMmoaK7 ...

  7. SQLite中命令行程序(CLP)的使用

    SQLite CLP是使用和管理SQLite数据库最常用的工具.它在所有平台上的操作方式相同.CLP其实是两个程序,它可以运行在Shell模式下以交互的方式执行查询操作,也可以运行在命令行模式下完成各 ...

  8. 小课堂week13 Clean Code Part2

    Clean Code Part2 对象与数据结构 首先让我们进行一个严肃的思考,对象与数据结构的区别在哪里? 如下两段代码分别用数据结构和对象的方法来描述了一个Point. public class ...

  9. nandflash操作详解

    1.nandflash就是嵌入式系统的硬盘 2.分类(1)MLC:存储单元格存储两位,慢,偏移,寿命短,容量大(2)SLC:存储一位.快,寿命长,容量小,昂贵 3访问:(1)独立编址,有专用的控制器, ...

  10. python杂记-4(迭代器&生成器)

    #!/usr/bin/env python# -*- coding: utf-8 -*-#1.迭代器&生成器#生成器#正确的方法是使用for循环,因为generator也是可迭代对象:g = ...