zoukankan      html  css  js  c++  java
  • [HZOI 2016]公路修建

    【题目描述】

    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条公路,满足上面的条件。

    【输入格式】

    第一行有三个数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的花费。

    【输出格式】

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

    【样例输入】

    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
    

    【样例输出】

    5

    题解:
    二分答案,强制只加<=mid的边,优先加k条一级公路,再加二级公路,期间判断能否凑足k条一级公路即可
     1 #include <algorithm>
     2 #include <iostream>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <cstdio>
     6 #include <cmath>
     7 using namespace std;
     8 const int N=10005,M=20005;
     9 int gi(){
    10     int str=0;char ch=getchar();
    11     while(ch>'9' || ch<'0')ch=getchar();
    12     while(ch>='0' && ch<='9')str=(str<<1)+(str<<3)+ch-48,ch=getchar();
    13     return str;
    14 }
    15 int n,m,k;
    16 struct node{
    17     int x,y,c1,c2;
    18     bool operator <(const node &pp)const{
    19         return c1<pp.c1;
    20     }
    21 }e[M];
    22 int fa[N];
    23 int find(int x){
    24     return fa[x]==x?x:fa[x]=find(fa[x]);
    25 }
    26 bool check(int lim){
    27     int x,y,cnt=0,kt=0,i;
    28     for(int i=1;i<=n;i++)fa[i]=i;
    29     for(i=1;i<=m;i++){
    30         if(e[i].c1>lim)break;
    31         x=e[i].x;y=e[i].y;
    32         if(find(x)==find(y))continue;
    33         kt++;cnt++;
    34         fa[find(y)]=find(x);
    35         if(cnt==n-1)break;
    36         if(kt==k)break;
    37     }
    38     if(kt<k)return false;
    39     if(cnt==n-1)return true;
    40     for(;i<=m;i++){
    41         if(e[i].c2>lim)continue;
    42         x=e[i].x;y=e[i].y;
    43         if(find(x)==find(y))continue;
    44         cnt++;fa[find(y)]=find(x);
    45         if(cnt==n-1)break;
    46     }
    47     return cnt==n-1;
    48 }
    49 void work(){
    50     n=gi();k=gi();m=gi();
    51     for(int i=1;i<m;i++){
    52         e[i].x=gi();e[i].y=gi();e[i].c1=gi();e[i].c2=gi();
    53     }
    54     sort(e+1,e+m);
    55     int l=1,r=30000,mid,ans;
    56     while(l<=r){
    57         mid=(l+r)>>1;
    58         if(check(mid))ans=mid,r=mid-1;
    59         else l=mid+1;
    60     }
    61     printf("%d
    ",ans);
    62 }
    63 int main()
    64 {
    65     freopen("hzoi_road.in","r",stdin);
    66     freopen("hzoi_road.out","w",stdout);
    67     work();
    68     return 0;
    69 }
     
  • 相关阅读:
    redis_ 5 集群
    redis_4 主从模式
    redis_3 持久化
    redis_2 数据类型
    linux_ubuntu 连接xftp
    redis_1 安装和简单使用
    Activiti 各个节点涉及的表
    oracle 数据库imp操作导入dmp文件时表空间问题
    ORA-27101: shared memory realm does not exist 错误的处理
    oralce清理user 和tablespace.
  • 原文地址:https://www.cnblogs.com/Yuzao/p/7213873.html
Copyright © 2011-2022 走看看