【最小生成树】BZOJ 1196: [HNOI2006]公路修建问题

1196: [HNOI2006]公路修建问题

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 1435  Solved: 810
[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

  一开始以为是权值和吓傻了
  结果再一看题目
  具体就是二分ans看剩下的边能不能构成生成树及一级公路数目是不是大于k
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<cmath>
 5 
 6 #define maxn 20001
 7 
 8 using namespace std;
 9 
10 inline int in()
11 {
12     int x=0;char ch=getchar();
13     while(ch<'0'||ch>'9')ch=getchar();
14     while(ch<='9'&&ch>='0')x=x*10+ch-'0',ch=getchar();
15     return x;
16 }
17 
18 struct ed{
19     int u,v,c,pos;
20 }edge1[maxn],edge2[maxn];
21 
22 bool cmp(const ed A,const ed B)
23 {
24     return A.c<B.c||(A.c==B.c&&A.pos<B.pos);
25 }
26 
27 int k,father[maxn],n,m;
28 
29 int find(int k){return father[k]==k?father[k]:father[k]=find(father[k]);}
30 
31 bool judge(int ans)
32 {
33     int cnt=0;
34     for(int i=1;i<=n;i++)father[i]=i;
35     for(int i=1;i<m;i++)
36         if(edge1[i].c<=ans)
37         {
38             int x1=find(father[edge1[i].u]);
39             int y1=find(father[edge1[i].v]);
40             if(x1!=y1)
41             {
42                 cnt++;
43                 father[x1]=y1;
44             }
45         }
46         else break;
47     if(cnt<k)return 0;
48     for(int i=1;i<m;i++)
49         if(edge2[i].c<=ans)
50         {
51             int x1=find(father[edge2[i].u]);
52             int y1=find(father[edge2[i].v]);
53             if(x1!=y1)
54             {
55                 cnt++;
56                 father[x1]=y1;
57             }
58         }
59         else break;
60     if(cnt!=n-1)return 0;
61     return 1;
62 }
63 
64 int main()
65 {
66     int l=1,r=0,ans;
67     n=in(),k=in(),m=in();
68     for(int i=1;i<m;i++)
69     {
70         edge1[i].u=in(),edge1[i].v=in(),edge1[i].c=in(),edge1[i].pos=i;
71         edge2[i].u=edge1[i].u,edge2[i].v=edge1[i].v,edge2[i].c=in(),edge2[i].pos=i;
72         r=max(r,edge1[i].c);
73     }
74     sort(1+edge1,edge1+m,cmp);
75     sort(1+edge2,edge2+m,cmp);
76     while(l<=r)
77     {
78         int mid=(l+r)>>1;
79         if(judge(mid))r=mid-1,ans=mid;
80         else l=mid+1;
81     }
82     printf("%d",ans);
83     return 0;
84 }
View Code
posted @ 2015-10-14 22:41  puck_just_me  阅读(173)  评论(0编辑  收藏  举报