HIT2739 the Chinese Postman problem (minimum cost maximum flow)

Source: Internet
Author: User

The topic probably said to a map, to start from 0 points to return 0 points and each side at least to walk once, beg to go the shortest distance.

For the classical CPP problem, the solution is to construct the Euler circuit by adding edges, and the sufficient and necessary condition for the existence of a Euclidean circuit is that the base diagram is connected and all the points are equal to the degree of the degree .

And this problem, decisively Lenovo to the mixed-graph Euler circuit approach, with the minimum cost maximum flow solution:

    • Consider all the edges only once, calculate the degrees and degrees of each point, the degree is not equal to the degree of the point of the need to select a few sides to change the adjustment of them
    • For the number of out-of-the-capacity network and the meeting point of the capacity-to-the-cost of 0 of the edge, more than the source point to the same edge
    • For all edges in the original image <u,v> by U to v capacity inf cost the side length of the edge, a unit of traffic flow through the edge will make the original degree of the U point of +1, the original degree of the V-point of more than +1
    • And then run the minimum cost maximum flow, if the edges associated with the Yuanhui are full, then there is a solution, and the final solution is the result of the length of all sides and the maximum flow of the minimum cost.

Also pay attention to determine the base diagram connectivity.

1#include <cstdio>2#include <cstring>3#include <queue>4#include <algorithm>5 using namespacestd;6 #defineINF (1&LT;&LT;30)7 #defineMAXN 1118 #defineMAXM 88889 structedge{Ten     intU,v,cap,cost,next; One }EDGE[MAXM]; A intVS,VT,NV,NE,HEAD[MAXN]; - voidAddedge (intUintVintCapintCost ) { -Edge[ne].u=u; Edge[ne].v=v; Edge[ne].cap=cap; edge[ne].cost=Cost ; theEdge[ne].next=head[u]; head[u]=ne++; -Edge[ne].u=v; Edge[ne].v=u; edge[ne].cap=0; edge[ne].cost=-Cost ; -EDGE[NE].NEXT=HEAD[V]; head[v]=ne++; - } + intD[MAXN],PRE[MAXN]; - BOOLVIS[MAXN]; + BOOLSPFA () { A      for(intI=0; i<nv; ++i) { atD[i]=inf; vis[i]=0; -     } -d[vs]=0; vis[vs]=1; -queue<int>que; - Que.push (VS); -      while(!Que.empty ()) { in         intu=Que.front (); Que.pop (); -          for(intI=head[u]; i!=-1; I=Edge[i].next) { to             intv=edge[i].v; +             if(Edge[i].cap && d[v]>d[u]+edge[i].cost) { -d[v]=d[u]+Edge[i].cost; thepre[v]=i; *                 if(!Vis[v]) { $vis[v]=1;Panax Notoginseng Que.push (v); -                 } the             } +         } Avis[u]=0; the     } +     returnd[vt]!=INF; - } $ intMCMF (int&Mxflow) { $     intres=0; -      while(SPFA ()) { -         intflow=inf,cost=0; the          for(intU=VT; U!=vs; u=edge[pre[u]].u) { -flow=min (flow,edge[pre[u]].cap);Wuyi         } themxflow-=flow; -          for(intU=VT; U!=vs; u=edge[pre[u]].u) { Wuedge[pre[u]].cap-=flow; -edge[pre[u]^1].cap+=flow; Aboutcost+=flow*Edge[pre[u]].cost; $         } -res+=Cost ; -     } -     returnRes; A } + intPAR[MAXN]; the intFind (intx) { -      while(x!=Par[x]) { $par[x]=Par[par[x]]; thex=Par[x]; the     } the     returnx; the } - BOOLUnion (intAintb) { in     intPa=find (a), pb=Find (b); the     if(PA==PB)return 0; thepar[pb]=PA; About     return 1; the } the intDEG[MAXN]; the intMain () { +     intt,n,m,a,b,c; -scanf"%d",&t); the      while(t--){Bayiscanf"%d%d",&n,&m); theVs=n; vt=vs+1; nv=vt+1; Ne=0; thememset (head,-1,sizeof(head)); -memset (deg,0,sizeof(deg)); -          for(intI=0; i<n; ++i) par[i]=i; the         intres=0, tot=n,mxflow=0; the          for(intI=0; i<m; ++i) { thescanf"%d%d%d",&a,&b,&c); the Addedge (a,b,inf,c); -res+=C; the++deg[a]; --Deg[b]; the             if(Union (b))--tot; the         }94         if(tot!=1){ thePuts"-1"); the             Continue; the         }98          for(intI=0; i<n; ++i) { About             if(deg[i]<0) Addedge (Vs,i,-deg[i],0); -             ElseAddedge (I,vt,deg[i],0), mxflow+=Deg[i];101         }102res+=MCMF (mxflow);103         if(mxflow!=0) puts ("-1");104         Elseprintf"%d\n", res); the     }106     return 0;107}

HIT2739 the Chinese Postman problem (minimum cost maximum 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.