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
*/
    原文作者:道路修建问题
    原文地址: https://blog.csdn.net/mdnd1234/article/details/65628389
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞