2012 National Team Tree mutual test

You give a free communication to FIG weighted, each edge is black or white. Let you find a minimum weight of exactly need spanning tree white side stripe. Topic guarantee solvability


 

This question is not very good feel called by generations also need cnt practice

Anyway, how are bzoj data on the water

#include<bits/stdc++.h>
#define re return
#define ll long long
#define inc(i,l,r) for(int i=l;i<=r;++i)

using namespace std;
template<typename T>inline void rd(T&x)
{
    char c;bool f=0;
    while((c=getchar())<'0'||c>'9')if(c=='-')f=1;
    x=c^48;
    while((c=getchar())>='0'&&c<='9')x=x*10+(c^48);
    if(f)x=-x;
}

const int maxn=50005,maxm=100005;
int n,m,sum,need,ans,hd[maxn],fa[maxn];

struct node{
    int fr,to,val,flag;
    bool operator<(node a)const 
    {
        if(val==a.val)re flag<a.flag;
        re val<a.val;
    }
}e[maxm];

inline int find ( int x) 
{ 
    D x == ago [x]? x ago [x] = find (for [x]); 
} 

Inline int vivi ( int x) 
{ 
    inc (and 1 , m) f (E [i] .flag) e [i] .val + = x; 
    
    inc (i, 1 , n) does [i] = i; 
    
    Luckily (e + 1 , m + e + 1 );
    int cnt = 0 , all = 0 ; 
    
    sum = 0 ; 
    
    Inc. (and 1 , m) 
    { 
        int f1 = find (e [i] .fr) f2 =find(e[i].to);
        if(f1!=f2)
        {
            ++tot;
            sum+=e[i].val;
            if(!e[i].flag)
                ++cnt;
            fa[f1]=f2;
            if(tot==n-1)break;
        }
    }
    
    inc(i,1,m)
    if(!e[i].flag)
        e[i].val-=x;
    if(cnt<need)re 0;
    ans=sum-cnt*x;
// but if need be the case, the original is not in the right side and it ranged Re
. 1 ; } int main () { The freopen ( " in.txt " , " R & lt " , stdin); RD (n-), RD (m ); RD (need); inc is (I, . 1 , m) { RD (E [I] .fr); RD (E [I] .to); ++ E [I] .fr; ++ E [I ] .to; RD (E [I] .val); RD (E [I] .flag); } int L = - 100 , R & lt = 100 ; the while (L <= R & lt) { int MID = (L + R & lt ) >> 1 ; IF(vivi(mid)) l=mid+1; else r=mid-1; } printf("%d",ans); re 0; }

 

Guess you like

Origin www.cnblogs.com/lsyyy/p/11411532.html