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

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std ;
#define MAXN 10010
#define MAXM 20010
int s[ MAXM ] , t[ MAXM ] , c1[ MAXM ] , c2[ MAXM ] , n , m , k ;
struct Uset {
    int father[ MAXN ] ;
    void Init(  ) {
        memset( father , 0 , sizeof( father ) ) ;
    }
    int Find( int x ) {
        int i = x , j = x ;
        for ( ; father[ i ] ; i = father[ i ] ) ;
        for ( ; father[ j ] ; ) {
            int k = father[ j ] ;
            father[ j ] = i ; 
            j = k ;
        }
        return i ;
    }
    bool check( int x , int y ) {
        return Find( x ) == Find( y ) ;
    }
    void Union( int x , int y ) {
        father[ Find( x ) ] = Find( y ) ;
    }
 
} u ;
bool Check( int x ) {
    u.Init(  ) ; 
    int cnt = 0 ; 
    for ( int i = 0 ; i ++ < m ; ) if ( ! u.check( s[ i ] , t[ i ] ) && c1[ i ] <= x ) {
        ++ cnt , u.Union( s[ i ] , t[ i ] ) ;
    }
    if ( cnt < k ) return false ;
    for ( int i = 0 ; i ++ < m ; ) if ( ! u.check( s[ i ] , t[ i ] ) && c2[ i ] <= x ) {
        u.Union( s[ i ] , t[ i ] ) , ++ cnt ;
    }
    for ( int i = 1 ; i ++ < n ; ) if ( ! u.check( i , i - 1 ) ) return false ;
    return true ;
}
int main(  ) {
    scanf( "%d%d%d" , &n , &k , &m ) ; -- m ;
    for ( int i = 0 ; i ++ < m ; ) scanf( "%d%d%d%d" , s + i , t + i , c1 + i , c2 + i ) ;
    int l = 0 , r = 30000 ;
    while ( r - l > 1 ) {
        int mid = ( l + r ) >> 1 ; 
        if ( Check( mid ) ) r = mid ; else l = mid ;
    }
    printf( "%d\n" , r ) ;
    return 0 ; 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值