zoukankan      html  css  js  c++  java
  • 公路修建问题(二分+最小生成树)

    Time Limit: 10 Sec  Memory Limit: 162 MB
    Submit: 2061  Solved: 1184

    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
     
    //二分+最小生成树,把两种路分开存的,这样是为了尽量先选一级公路,然后选二级公路
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <algorithm>
     5 
     6 using namespace std;
     7 #define MAXN 10005
     8 #define MAXM 20005
     9 
    10 struct Node
    11 {
    12     int s,e;
    13     int v;
    14     bool operator < (const Node& b)const {return v<b.v;}
    15 }r1[MAXM],r2[MAXM];
    16 
    17 int n,k,m,r1_n,r2_n;
    18 int f[MAXN];
    19 
    20 int Find(int x)
    21 {
    22     if (x!=f[x])
    23         f[x]=Find(f[x]);
    24     return f[x];
    25 }
    26 
    27 int krus(int cost)
    28 {
    29     for (int i=1;i<=n;i++) f[i]=i;
    30 
    31     int road=0;
    32     int L = 1;
    33 
    34     for (int i=0;i<r1_n;i++)    //一级公路
    35     {
    36         if (r1[i].v>cost) break;
    37         int xx = Find(r1[i].s);
    38         int yy = Find(r1[i].e);
    39         if (f[xx]!=f[yy])
    40         {
    41             road++;
    42             f[xx]=f[yy];
    43             L++;    //连通的个数
    44         }
    45         if (L==n) break;
    46     }
    47     for (int i=0;i<r2_n;i++)    //二级公路
    48     {
    49         if (r2[i].v>cost) break;
    50         int xx = Find(r2[i].s);
    51         int yy = Find(r2[i].e);
    52         if (f[xx]!=f[yy])
    53         {
    54             f[xx]=f[yy];
    55             L++;    //连通的个数
    56         }
    57         if (L==n) break;
    58     }
    59     if (road>=k&&L==n)
    60         return 1;
    61     return 0;
    62 }
    63 
    64 int main()
    65 {
    66     scanf("%d%d%d",&n,&k,&m);
    67     r1_n=0;
    68     r2_n=0;
    69     for (int i=0;i<m-1;i++)
    70     {
    71         int a,b,c1,c2;
    72         scanf("%d%d%d%d",&a,&b,&c1,&c2);
    73         r1[r1_n++]=(Node){a,b,c1};
    74         r2[r2_n++]=(Node){a,b,c2};
    75     }
    76     sort(r1,r1+r1_n);
    77     sort(r2,r2+r2_n);
    78 
    79     int l=0,r=30000;
    80     int ans;
    81 
    82     while (l<=r)
    83     {
    84         int mid = (l+r)/2;
    85         if (krus(mid))
    86         {
    87             r=mid-1;
    88             ans=mid;
    89         }
    90         else
    91             l=mid+1;
    92     }
    93     printf("%d
    ",ans);
    94 
    95     return 0;
    96 }
    View Code
  • 相关阅读:
    Qt uchar * 转 QImage
    WIN10 蓝牙连接音箱之后,音量调节无效,音量从1-100,声音一样大,都是最大声,可以静音(解决方案)
    OpenGL 保存bmp图像
    Qt 使用自带的OpenGL模块开发程序
    OpenCVSharp介绍
    OpenCV介绍
    OpenAL介绍
    OpenCL介绍
    OpenGL介绍
    Qt 无法打开包括文件:“QGLWidget”: No such file or directory
  • 原文地址:https://www.cnblogs.com/haoabcd2010/p/6613680.html
Copyright © 2011-2022 走看看