BZOJ P1196:[HNOI2006]公路修建问题

直接二分答案然后判断即可

下面是代码

能过算我输

#include<iostream>
#include<algorithm>
using namespace std;
int n,k,m,cnt,mx;
int u[20003],v[20003],c1[20003],c2[20003],fa[10003];
int find(int x){
	if(x==fa[x]){
		return x;
	}
	return fa[x]=find(fa[x]);
}
bool check(int hehe){
	for(int i=1;i<=m;i++){
		fa[i]=i;cnt=0;
	}
	for(int i=1;i<=m;i++){
		if(c1[i]<=hehe){
        	int x=find(u[i]),y=find(v[i]);
			if(x==y){
				continue;
			}
			fa[y]=x;cnt++;
    	}
	}
    if(cnt<k){
    	return 0;
	}
	for(int i=1;i<=m;i++){
		if(c2[i]<=hehe){
        	int x=find(u[i]),y=find(v[i]);
			if(x==y){
				continue;
			}
			fa[y]=x;cnt++;
			if(cnt>=n-1){
				return 1;
			}
    	}
	}
	return 0;
}
int main(){
    cin>>n>>k>>m;
	mx=0;
	for(int i=1;i<=m-1;i++){
		cin>>u[i]>>v[i]>>c1[i]>>c2[i];
		mx=max(mx,c1[i]);mx=max(mx,c2[i]);
	}
    int l=0,r=mx,ans;
    while(l<=r){
        int mid=(l+r)>>1;
        if(check(mid)){
        	//ans=mid;
			r=mid-1;
		}else{
			l=mid+1;
		}
    }
    cout<<l<<endl;
	return 0;
}
/*
in:
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

out:
5
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值