bzoj2561 minimum Spanning Tree

Source: Internet
Author: User

bzoj2561 minimum Spanning Tree

Test instructions

Given a connected undirected graph, it is assumed that by adding an edge (u,v) with a side right of L, it is necessary to delete the minimum number of edges in order to be able to make the edge appear on either the smallest spanning tree or the largest spanning tree.

Exercises

Minimum cut. If an edge appears on the smallest spanning tree, then the edge of the weight that is smaller than it must not be able to connect the graph. Since the minimum requirement is deleted, the entire graph is connected after joining this side. Therefore, you can use one end of the edge as the source, the other end as a sink, insert so that the weight than L small edge, each side of the flow of 1, run the smallest cut, the answer is to make the source, sinks do not connect at least delete edge. The largest spanning tree, in the same vein, inserts a weight greater than L. The final answer is the sum of the results of the minimum cut of two runs. Reflection: Note that the edge to open to 4 times times, and the edge of the picture is no direction, in the network flow inserted in the edge of two directions. This problem also tells us that the actual data range of tens of thousands may also use the network stream. The metaphysical complexity of DINIC/ISAP qaq

Code:

1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <queue>5 #defineMAXN 300006 #defineInc (I,J,K) for (int i=j;i<=k;i++)7 #defineINF 0X3FFFFFFF8 using namespacestd;9 Ten structe{intT,c,n;}; E es[maxn* +];intg[maxn],ess; OneInlinevoidPeintFintTintc) { AEs[++ess]= (e) {t,c,g[f]}; g[f]=ess; Es[++ess]= (e) {f,0, G[t]}; g[t]=ess; - } -Inlinevoidinit () { theess=-1; memset (g,-1,sizeof(g)); - } -Queue <int> q;intH[MAXN]; - BOOLBFsintSintt) { +memset (h,-1,sizeof(h)); while(!q.empty ()) Q.pop (); h[s]=0; Q.push (s); -      while(!Q.empty ()) { +         intx=Q.front (); Q.pop (); A          for(inti=g[x];i!=-1; i=es[i].n)if(es[i].c&&h[es[i].t]==-1) h[es[i].t]=h[x]+1, Q.push (es[i].t); at     } -     returnh[t]!=-1; - } - intDfsintXintTintf) { -     if(x==t)returnFintu=0; -      for(inti=g[x];i!=-1; i=es[i].n)if(es[i].c&&h[es[i].t]==h[x]+1){ in         intW=dfs (Es[i].t,t,min (F,ES[I].C)); F-=w; U+=w; Es[i].c-=w; es[i^1].c+=w;if(f==0)returnu; -     } to     if(u==0) h[x]=-1;returnu; + } - intDinic (intSintt) { the     intf=0; while(BFS (s,t)) F+=dfs (S,t,inf);returnF; * } $ intn,m,u[maxn*Ten],v[maxn*Ten],w[maxn*Ten],u,v,l;Panax Notoginseng intMain () { -scanf"%d%d", &n,&m); Inc (I,1, m) scanf ("%d%d%d", &u[i],&v[i],&w[i]); scanf"%d%d%d", &u,&v,&l);intans=0; theInit (); Inc (I,1, m)if(w[i]<l) PE (u[i],v[i],1), PE (v[i],u[i],1); ans+=dinic (u,v); +Init (); Inc (I,1, m)if(w[i]>l) PE (u[i],v[i],1), PE (v[i],u[i],1); ans+=dinic (u,v); Aprintf"%d", ans);return 0; the}

20160523

bzoj2561 minimum Spanning Tree

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.