zoukankan      html  css  js  c++  java
  • BZOJ2654

    Portal

    Description

    给出一个无向连通图((V,E))(|V|leq5 imes10^4,|E|leq10^5)),边分为白边和黑边,边权不超过(100)。求该无向图的恰好包含(k)条白边的最小生成树的权值。保证有解。

    Solution

    二分。
    我们给所有白边一个额外权值(w')。显然MST中白边的数量与(w')负相关,于是我们二分这个(w')
    可以证明,加上(w')后的一个包含(x)条白边的MST,就是原图中包含(x)条白边的最小生成树。因为所有包含(x)条白边的生成树在此时的权值都加上了(w'x),所以其在原图中仍然最小。那么我们只要找到一棵包含(k)条边的生成树即可。
    不过对于一个(w'),不同的MST包含的白边数可能不同。我们可以优先白边求一个MST,其包含(kR)条白边;优先黑边求一个MST,其包含(kL)条白边。那么该图的所有MST中的白边数都在([kL,kR])区间内,且每种白边数的MST都存在[1]。那么当(kin[kL,kR])时我们也找到了一棵包含(k)条边的生成树。

    时间复杂度(O(log(100|E|)cdot |E|log |E|)=O(|E|log^2|E|))

    Code

    //tree
    #include <algorithm>
    #include <cstdio>
    using namespace std;
    const double EPS=1e-4;
    inline char gc()
    {
        static char now[1<<16],*s,*t;
        if(s==t) {t=(s=now)+fread(now,1,1<<16,stdin); if(s==t) return EOF;}
        return *s++;
    }
    inline int read()
    {
        int x=0; char ch=gc();
        while(ch<'0'||'9'<ch) ch=gc();
        while('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=gc();
        return x;
    }
    inline bool equal(double x,double y) {return -EPS<x-y&&x-y<=EPS;}
    const int N=5e4+10;
    int n,m,k;
    struct edge{int u,v,w0,c; double w;} ed[N*2];
    double ans;
    bool cmpW0(edge x,edge y) {return equal(x.w,y.w)?x.c<y.c:x.w<y.w;}
    bool cmpW1(edge x,edge y) {return equal(x.w,y.w)?x.c>y.c:x.w<y.w;}
    double res;
    int pre[N];
    int find(int x) {return x==pre[x]?x:pre[x]=find(pre[x]);}
    int Kruskal()
    {
        res=0; int kCnt=0,trCnt=0;
        for(int i=1;i<=n;i++) pre[i]=i;
        for(int i=1;i<=m;i++)
        {
            int u=ed[i].u,v=ed[i].v;
            if(find(u)!=find(v))
            {
                res+=ed[i].w; if(ed[i].c==0) kCnt++; trCnt++;
                pre[find(u)]=find(v);
            }
            if(trCnt==n-1) return kCnt;
        }
    }
    void bSearch(double L,double R)
    {
        if(ans) return;
        double mid=(L+R)/2;
        for(int i=1;i<=m;i++) if(ed[i].c==0) ed[i].w=ed[i].w0-mid;
        int kL,kR;
        sort(ed+1,ed+m+1,cmpW0); kR=Kruskal();
        sort(ed+1,ed+m+1,cmpW1); kL=Kruskal();
        if(kL<=k&&k<=kR) {ans=res+k*mid; return;}
        if(k<kL) bSearch(L,mid);
        if(kR<k) bSearch(mid,R);
    }
    int main()
    {
        n=read(),m=read(),k=read();
        for(int i=1;i<=m;i++)
        {
            ed[i].u=read()+1,ed[i].v=read()+1;
            ed[i].w=ed[i].w0=read(),ed[i].c=read();
        }
        bSearch(-10,10);
        printf("%.0lf
    ",ans);
        return 0;
    }
    

    P.S.

    说是二分(100|E|)其实二分([-10,10])就能过,并没有特别极限的数据。
    或许只在([-10,10])里二分整数也可以,因为如果二分到小数就没有权值相同的边,一定有(kL=kR)


    1. 权值相同的生成树可以通过加边((u,v))再删掉路径((u,v))上的一条权值相同的边来互相转化。那么我们可以添加白边再删掉权值相同的黑边。(以上均为口胡,没有严谨证明) ↩︎

  • 相关阅读:
    买房的贷款时间是否是越长越好?https://www.zhihu.com/question/20842791
    asp.net cookie and session
    leelazero and google colab
    download file by python in google colab
    physical processor, core, logical processor
    通过powershell操作eventlog
    openxml in sql server
    get the page name from url
    How to Execute Page_Load() in Page's Base Class?
    Difference between HttpContext.Request and Request
  • 原文地址:https://www.cnblogs.com/VisJiao/p/BZOJ2654.html
Copyright © 2011-2022 走看看