hdu4253 dichotomy +mst (classic model)

Source: Internet
Author: User

N points, M edge, the edge is divided into a, b two classes, to construct a minimum spanning tree, and the number of a side of the tree is K.

We can control the number of a edges in the tree by adding a weight dx to all a edges. Obviously, when the DX increases, the A-side number KK will decrease.

Two-minute DX,

When kk>=k, increase the DX (i.e. l=mid+1) while updating the Ans=sum (MST)-mid*k;

When kk<k, reduce DX (i.e. r=mid-1).

1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 structnode6 {7     intu,v,w;8}ey[110000],el[110000];9 intn,m,k,numey,numel,sset[110000],sum;Ten Const intmmax=1<< -; One intFfind (intx) A { -     if(sset[x]==x)returnx; -     intFa=Sset[x]; thesset[x]=Ffind (FA); -     returnSset[x]; - } - voidMmerge (intAintb) + { -     intAa=Ffind (a); +     intbb=Ffind (b); A     if(aa!=BB) atsset[aa]=BB; - } - BOOLCMP (node X1,node y1) - { -     returnx1.w<Y1.W; - } in intKruscal (intDX) - { to     inti; +sum=0; -      for(i=0; i<=n;i++) thesset[i]=i; *     intkk=0, posey=0, posel=0, num=0; $      while(posey<numey| | posel<Numel)Panax Notoginseng     { -         int  from, to; the         if(ey[posey].w+dx<=EL[POSEL].W) +         { A              from=Ffind (ey[posey].u); theto=Ffind (EY[POSEY].V); +             if( from!=to ) -             { $ Mmerge (EY[POSEY].U,EY[POSEY].V); $sum+= (ey[posey].w+dx); -kk++; -num++; the             } -posey++;Wuyi         } the         Else -         { Wu              from=Ffind (el[posel].u); -to=Ffind (EL[POSEL].V); About             if( from!=to ) $             { - Mmerge (EL[POSEL].U,EL[POSEL].V); -sum+=EL[POSEL].W; -num++; A             } +posel++; the         } -         if(num==n-1) Break; $     } the     if(kk>=k)return 1; the     Else return 0; the } the intMain () - { in     inti,casnum=0; the      while(SCANF ("%d%d%d", &n,&m,&k)! =EOF) the     { AboutNumey=numel=0; the          for(i=0; i<m;i++) the         { the             inta,b,c,tmp; +scanf"%d%d%d%d",&a,&b,&c,&tmp); -             if(tmp==0) the             {Bayiey[numey].u=A; theey[numey].v=b; theey[numey++].w=C; -             } -             Else the             { theel[numel].u=A; theel[numel].v=b; theel[numel++].w=C; -             } the         } theSort (ey,ey+numey,cmp); theSort (el,el+numel,cmp);94ey[numey].w=el[numel].w=Mmax; the         intl=- -, r= -, mid,ans=0; the          while(l<=R) the         {98Mid= (L+R)/2; About             if(Kruscal (mid)) -             {101L=mid+1;102ans=sum-mid*K;103             }104             Elser=mid-1; the         }106printf"Case %d:%d\n",++Casnum,ans);107     }108     return 0;109}

hdu4253 dichotomy +mst (classic model)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.