The Chinese Postman problem 2739 (the problem of the map China postman)

Source: Internet
Author: User



The problem of the graph, if each point is an even number of degrees, is the Euler loop, and for a point only two cases, odd and even, then the odd pair of points with a edge right for the original two points of the shortest value of the edge is not good



No-map China postman problem:















The problem of the direction diagram, if each point in the degree and out of the same, then is the Euler circuit, and this situation is more, the same, less than one, into the degree of less than two, the degree of less than 1, the degree of less two,



Now, if we put the degree of less and out of the connection is not the Euler circuit, such as the point X of the degree of 7, the degree of 3, point y of the degree of 2, into the degree of 4, point Z of the degree of 2, into the degree of 4;



So X is a point y or Point Z, of course, the smallest distance of the first, assuming y, then x <-y with two edges, X into the degree of 7, into the degree of 5,y and the degree of the same,



Then X will start even Z, think about this is not the cost of the flow, first make the road cost small full flow, and then small, and then small, so the cost flow can be a perfect solution to this problem



The problem of China's postman with the map:












Cough ... Anyway wrong online code also wrong


#include <iostream>#include<cstdio>#include<sstream>#include<cstring>#include<map>#include<cctype>#include<Set>#include<vector>#include<stack>#include<queue>#include<algorithm>#include<cmath>#include<bitset>#defineRap (i, A, n) for (int i=a; i<=n; i++)#defineRep (I, A, n) for (int i=a; i<n; i++)#defineLap (I, a, n) for (int i=n; i>=a; i--)#defineLEP (i, A, n) for (int i=n; i>a; i--)#defineRD (a) scanf ("%d", &a)#defineRlld (a) scanf ("%lld", &a)#defineRC (a) scanf ("%c", &a)#defineRS (a) scanf ("%s", a)#definePD (a) printf ("%d\n", a);#definePlld (a) printf ("%lld\n", a);#definePC (a) printf ("%c\n", a);#definePS (a) printf ("%s\n", a);#defineMOD 2018#defineLL Long Long#defineULL unsigned long Long#definePair Pair<int, int>#defineMem (A, B) memset (A, B, sizeof (a))#define_ Ios_base::sync_with_stdio (0), Cin.tie (0)//freopen ("1.txt", "R", stdin);using namespacestd;Const intMAXN =30010, INF =0x7fffffff, Ll_inf =0x7fffffffffffffff;intN, m, S, T;intHEAD[MAXN], D[MAXN], VIS[MAXN], P[MAXN], F[MAXN], FI[MAXN];int inch[MAXN], out[MAXN];intcnt, flow, value;structnode{intu, V, C, W, next;} NODE[MAXN<<1];voidAddintUintVintCintW) {node[cnt].u=u; NODE[CNT].V=v; NODE[CNT].W=W; NODE[CNT].C=C; Node[cnt].next=Head[u]; Head[u]= cnt++;}intSPFA () {Queue<int>Q; Mem (Vis,0); MEM (P,-1);  for(inti =0; i < MAXN; i++) D[i] =INF;    Q.push (s); D[s]=0; Vis[s]=1; P[s]=0, f[s] =INF;  while(!Q.empty ()) { intU =Q.front ();        Q.pop (); Vis[u]=0;  for(inti = Head[u]; I! =-1; i =Node[i].next) {Node E=Node[i]; if(D[E.V] > D[u] + e.w && e.c >0) {D[E.V]= D[u] +E.W; P[E.V]=i; F[E.V]=min (f[u], e.c); if(!VIS[E.V])                    {Q.push (E.V); VIS[E.V]=1; }            }        }    } if(P[t] = =-1)return 0; Flow+ = F[t]; Value + = f[t] *D[t];  for(inti = t; I! = s; i =node[p[i]].u) {NODE[P[I]].C-=F[t]; Node[p[i]^1].C + =F[t]; } return 1;}voidMax_flow () { while(SPFA ());}voidinit () {mem (head,-1); MEM (inch,0); MEM ( out,0); CNT= value = Flow =0;}intFindintx) { returnFI[X] = = x? FI[X]: (fi[x] =find (Fi[x]));}intMain () {intT; intu, V, W; CIN>>T;  while(t--)    {  for(inti =0; i < MAXN; i++) Fi[i] =i; intFlag =0, ans =0;        Init (); intEdge_sum =0; CIN>> N >>m; S= n +1, T = n +2;  for(inti =0; I < m; i++) {cin>> u >> v >>W; intL =find (U); intR =Find (v); if(l! = r) Fi[l] =R; Edge_sum+=W;            Add (U, V, INF, W); inch[v]++;  out[u]++; }  for(inti =0; I < n; i++) if(Fi[i] = = i) ans++; if(Ans >1) {puts ("-1"); Continue; } intTot_flow =0;  for(inti =0; I < n; i++)        { if(inch[I] = =0&& out[I] = =0) {flag=1;  Break; } if( out[I] >inch[i]) Add (I, T, out[I]-inch[I],0), Tot_flow + = out[I]-inch[i]; Else if(inch[I] > out[i]) Add (s, I,inch[I]- out[I],0); } if(flag) {puts ("-1"); Continue;        } max_flow (); if(Tot_flow! =flow) {Puts ("-1"); Continue; } cout<< Edge_sum + value <<Endl; } return 0;}





The Chinese Postman problem 2739 (the problem of the map China postman)


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.