"UVA1658 Algorithm Competition Primer classic" Admiral "Fee Flow"

Source: Internet
Author: User

Test instructions

Gives a weighted graph of the edge of a V (3<=v<=1000) point E (3<=e<=10000), seeking the path of the two disjoint (except for the point of origin and end point) of the 1-v, which makes the right and the smallest.

Analysis

One of the classic uses of the expense flow is to limit the absence of a common edge, but there is a difference in this question, which limits the absence of a common point. So we take each point out of one side.

The 2 to V-1 each node i is split into I and I ' two nodes, the middle of a capacity of 1, the cost of 0 side. For each edge in the original image (A, A, b), a single arc (a ', a), a capacity of 1, the cost is the weight and then 1 to V for the flow of 2 of the minimum cost flow can be.

1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <iostream>5#include <queue>6 7 using namespacestd;8 Const intmaxn=4000+Ten;9 Const intmaxm=40000+Ten;Ten Const intinf=2147480000; One structmcmf{ A     intHEAD[MAXN],NEXT[MAXM],TO[MAXM], from[MAXM],FLOW[MAXM],CAP[MAXM],COST[MAXM]; -     intINQ[MAXN],D[MAXN],P[MAXN],A[MAXN]; -     intN,m,s,t,sz; the     voidInitintN) { -          This->n=N; -sz=-1; -memset (head,-1,sizeof(head)); +     } -     voidAdd_edge (intAintBintCaintCO) { +++sz; A          from[Sz]=a;to[sz]=b; next[sz]=head[a];head[a]=sz; atcap[sz]=ca;cost[sz]=co;flow[sz]=0; -++sz; -          from[Sz]=b;to[sz]=a; next[sz]=head[b];head[b]=sz; -cap[sz]=ca;cost[sz]=-co;flow[sz]=CA; -     } -     BOOLSPFA (intSintTint&flow,Long Long&Cost ) { in          for(intI=0; i<=n;i++) d[i]=INF; -queue<int>Q; tomemset (INQ,0,sizeof(INQ)); +d[s]=0; inq[s]=1;p [s]=-1; a[s]=INF; - Q.push (s); the          while(!Q.empty ()) { *             intu=Q.front (); Q.pop (); $inq[u]=0;Panax Notoginseng              for(inti=head[u];i!=-1; i=Next[i]) { -                 intv=To[i]; the                 if(cap[i]>flow[i]&&d[v]>d[u]+Cost[i]) { +d[v]=d[u]+Cost[i]; Ap[v]=i; theA[v]=min (a[u],cap[i]-flow[i]); +                     if(!Inq[v]) { -inq[v]=1; $ Q.push (v); $                     } -                 } -             } the         } -         if(D[t]==inf)return false;Wuyiflow+=A[t]; thecost+= (Long Long) a[t]* (Long Long) d[t]; -         intu=T; Wu          while(u!=s) { -flow[p[u]]+=A[t]; Aboutflow[p[u]^1]-=A[t]; $u= from[P[u]]; -         } -         return true; -     } A     Long LongMincost (intSintt) { +         intflow=0; the         Long Longcost=0; -          while(SPFA (S,t,flow,cost)); $         returnCost ; the     } the }MCMF; the intn,m; the intMain () { -      while(SCANF ("%d%d", &n,&m)! =EOF) { in         inta,b,c; theMcmf.init (2*n); the          for(intI=2; i<n;i++){ AboutMcmf.add_edge (I,i+n,1,0); the         } theMcmf.add_edge (1, n+1,2,0); theMcmf.add_edge (N,2*n,2,0); +          for(intI=1; i<=m;i++){ -scanf"%d%d%d",&a,&b,&c); theMcmf.add_edge (A+n,b,1, c);Bayi         } the         intANS=MCMF. Mincost (1,2*n); thecout<<ans<<Endl; -     } - return 0; the}
View Code

"UVA1658 Algorithm Competition Primer classic" Admiral "Fee Flow"

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.