首页 技术 正文
技术 2022年11月11日
0 收藏 444 点赞 2,670 浏览 2743 个字

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 由于是最小化最大值,我们可以考虑二分答案,将问题转化为判定问题。我们怎么判定一个答案x是否合法呢?考虑先将所有c1值小于等于x的边加进去,看看是否有至少k条边。再将所有c2值小于等于x的边加进去,看看是不是能构成一棵生成树。如果用路径压缩有一些慢(216ms)

#include<cstdio>
#include<cctype>
#include<queue>
#include<cstring>
#include<algorithm>
#define rep(s,t) for(int i=s;i<=t;i++)
#define ren for(int i=first[x];i!=-1;i=next[i])
using namespace std;
inline int read() {
int x=,f=;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-;
for(;isdigit(c);c=getchar()) x=x*+c-'';
return x*f;
}
const int maxn=;
int n,k,m,pa[maxn];
int findset(int x) {return x==pa[x]?x:pa[x]=findset(pa[x]);}
struct Edge {int u,v,w1,w2;}e[maxn];
int merge(int x,int y) {
x=findset(x);y=findset(y);pa[x]=y;
return x!=y;
}
int check(int x) {
rep(,n) pa[i]=i;
int tot=,cnt=n;
rep(,m) if(e[i].w1<=x) if(merge(e[i].u,e[i].v)) tot++;
cnt-=tot;if(tot<k) return ;
rep(,m) if(e[i].w2<=x) if(merge(e[i].u,e[i].v)) cnt--;
return cnt==;
}
int main() {
n=read();k=read();m=read()-;
rep(,m) e[i].u=read(),e[i].v=read(),e[i].w1=read(),e[i].w2=read();
int l=,r=,mid;
while(l<r) if(check(mid=l+r>>)) r=mid; else l=mid+;
printf("%d\n",l);
return ;
}

如果用按秩合并的办法竟然出奇地快(96ms)

#include<cstdio>
#include<cctype>
#include<queue>
#include<cstring>
#include<algorithm>
#define rep(s,t) for(int i=s;i<=t;i++)
#define ren for(int i=first[x];i!=-1;i=next[i])
using namespace std;
inline int read() {
int x=,f=;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-;
for(;isdigit(c);c=getchar()) x=x*+c-'';
return x*f;
}
const int maxn=;
int n,k,m,pa[maxn],rk[maxn];
int findset(int x) {return x==pa[x]?x:findset(pa[x]);}
struct Edge {int u,v,w1,w2;}e[maxn];
int merge(int x,int y) {
x=findset(x);y=findset(y);if(x==y) return ;
if(rk[x]>rk[y]) swap(x,y);
pa[x]=y;if(rk[x]==rk[y]) rk[y]++;
return ;
}
int check(int x) {
rep(,n) pa[i]=i,rk[i]=;
int tot=,cnt=n;
rep(,m) if(e[i].w1<=x) if(merge(e[i].u,e[i].v)) tot++;
cnt-=tot;if(tot<k) return ;
rep(,m) if(e[i].w2<=x) if(merge(e[i].u,e[i].v)) cnt--;
return cnt==;
}
int main() {
n=read();k=read();m=read()-;
rep(,m) e[i].u=read(),e[i].v=read(),e[i].w1=read(),e[i].w2=read();
int l=,r=,mid;
while(l<r) if(check(mid=l+r>>)) r=mid; else l=mid+;
printf("%d\n",l);
return ;
}
上一篇: mergeSort
下一篇: kvm安装
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,965
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,781