BZOJ P1196 [HNOI2006]公路修建问题【最小生成树】【二分答案】

二分一下答案然后生成树随便验证一下:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define DB double
#define SG string
#define LL long long
#define Fp(A,B,C,D) for(A=B;A<=C;A+=D)
#define Fm(A,B,C,D) for(A=B;A>=C;A-=D)
#define Clear(A) memset(A,0,sizeof(A))
using namespace std;
const LL Max=1e5+5;
const LL Mod=1e9+7;
const LL Inf=1e18;
struct Node{
    LL X,Y,V1,V2;
}G[Max<<1];
LL N,K,M,Ans,F[Max];
inline LL Read(){
    LL X=0;char CH=getchar();bool F=0;
    while(CH>'9'||CH<'0'){if(CH=='-')F=1;CH=getchar();}
    while(CH>='0'&&CH<='9'){X=(X<<1)+(X<<3)+CH-'0';CH=getchar();}
    return F?-X:X;
}
inline void Write(LL X){
    if(X<0)X=-X,putchar('-');
    if(X>9)Write(X/10);
    putchar(X%10+48);
}
LL Find(LL X){
    return X==F[X]?X:F[X]=Find(F[X]);
}
bool Check(LL X){
    LL I,J,Cnt=0,Tot;
    Fp(I,1,N,1){
        F[I]=I;
    }
    Fp(I,1,M,1){
        if(G[I].V1>X)
            continue;
        LL Fx=Find(G[I].X),Fy=Find(G[I].Y);
        if(Fx!=Fy){
            F[Fx]=Fy;
            Cnt++;
        }
    }
    if(Cnt<K){
        return 0;
    }
    Fp(I,1,M,1){
        if(G[I].V2>X)
            continue;
           
        LL Fx=Find(G[I].X),Fy=Find(G[I].Y);
        if(Fx!=Fy){
            F[Fx]=Fy;
            Cnt++;
        }
    }
    if(Cnt!=N-1){
        return 0;
    }return 1;
}
int main(){
    LL I,J;
    N=Read(),K=Read(),M=Read();
    Fp(I,1,M-1,1){
        G[I].X=Read(),G[I].Y=Read();
        G[I].V1=Read(),G[I].V2=Read();
    }LL Left=1,Right=30000;
    while(Left<=Right){
        LL Mid=(Left+Right)>>1;
        if(Check(Mid)){
            Ans=Mid;
            Right=Mid-1;
        } else {
            Left=Mid+1;
        }
    }Write(Ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yanzhenhuai/article/details/82794148
今日推荐