BZOJ-1196: [HNOI2006]公路修建问题 (二分+kruskal)

1196: [HNOI2006]公路修建问题

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 2437  Solved: 1398
[Submit][Status][Discuss]

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

傻逼最小生成树……为了AC率laj先测试了luogu的数据,结果聪明反被聪明误,luogu和BZOJ的题面虽然是一样的但是输入是不一样的_(:зゝ∠)_ 于是laj对着一个AC的程序挑错,调了半天也没调出来mmp _(:зゝ∠)_ 后来放到BZOJ上测结果一遍过 _(:зゝ∠)_

 1 #include "bits/stdc++.h"
 2 using namespace std;
 3 typedef long long LL;
 4 const int MAX1=1e4+5;
 5 const int MAX2=2e4+5;
 6 int n,m,K,fa[MAX1],ans;
 7 struct Edge{int u,v,w1,w2;}edge[MAX2];
 8 int getfather(int x){return fa[x]==x?x:fa[x]=getfather(fa[x]);}
 9 inline int read(){
10     int an=0,x=1;char c=getchar();
11     while (c<'0' || c>'9') {if (c=='-') x=-1;c=getchar();}
12     while (c>='0' && c<='9') {an=an*10+c-'0';c=getchar();}
13     return an*x;
14 }
15 bool feasible(int x){
16     int i,j;
17     int tx,ty,cnt=0,tmp=0;
18     for (i=1;i<=n;i++) fa[i]=i;
19     for (i=1;i<=m;i++){
20         if (edge[i].w1>x) continue;
21         tx=getfather(edge[i].u);
22         ty=getfather(edge[i].v);
23         if (tx!=ty){
24             fa[tx]=ty;
25             cnt++,tmp++;
26         }
27     }
28     if (cnt<K) return false;
29     for (i=1;i<=m;i++){
30         if (edge[i].w2>x) continue;
31         tx=getfather(edge[i].u);
32         ty=getfather(edge[i].v);
33         if (tx!=ty){
34             fa[tx]=ty;
35             tmp++;
36         }
37     }
38     return (tmp>=(n-1));
39 }
40 int main(){
41     freopen ("build.in","r",stdin);freopen ("build.out","w",stdout);
42     int i,j,low=1e9,high=0,mid;
43     n=read(),K=read(),m=read();m--;
44     for (i=1;i<=m;i++){
45         edge[i].u=read(),edge[i].v=read();
46         edge[i].w1=read(),edge[i].w2=read();
47         low=min(low,min(edge[i].w1,edge[i].w2));
48         high=max(high,max(edge[i].w1,edge[i].w2));
49     }
50     while (low<=high){
51         mid=(low+high)>>1;
52         if (feasible(mid)) ans=mid,high=mid-1;
53         else low=mid+1;
54     }
55     printf("%d",ans);
56     return 0;
57 }
未来是什么样,未来会发生什么,谁也不知道。 但是我知道, 起码从今天开始努力, 肯定比从明天开始努力, 要快一天实现梦想。 千里之行,始于足下! ——《那年那兔那些事儿》
原文地址:https://www.cnblogs.com/keximeiruguo/p/7709370.html