Chinese Postman Problem Aizu-dpl_2_b (No map China postman problem)

Source: Internet
Author: User

Test instructions

The problem of China's postal route on the graph with right and no direction: a postman needs to go through each side at least once, finally back to the starting point, a side repeatedly through the weights to accumulate, ask the minimum total weight value is how much. (2 <= N <=, 1 <= M <= 1000)

Analytical:

Each side must pass at least once, if each side can only pass once, then is not a problem of the Euler circuit, but the non-graph of the Eulerian circuit must ensure that each point is an even number of degrees

So if there is a certain point of the number of degrees is odd, it is more awkward ...

Because an edge has two endpoints, if there are odd points, then the number of odd points must be an even

Let's assume that there are two singularities in this graph that are s and T, then the Euler path s must go through all the sides once and then to T, but we still have to go back, so the shortest way to get to S is OK.

Then the shortest-circuit algorithm to find out that the original is T-s the shortest circuit added to the original image, is not a European pull circuit!

Similarly, if the number of odd points is greater than 2, then we will build a binary map, put these points to the left and right, each of the two points of the Benquan for the shortest between them, to find the minimum right matching is good

And then add the matching to the original to find the Euler circuit.

But the problem of the N relatively small to use the pressure to enumerate all the cases DP just a little bit better

  

#include <bits/stdc++.h>#defineMem (A, B) memset (A, B, sizeof (a))using namespacestd;Const intMAXN =10010, INF =0x7fffffff;intHEAD[MAXN], D[MAXN], VIS[MAXN], DEG[MAXN], dp[1<< the+1];intN, M, Cnt;vector<int>Odd;intway[ -][ -];structnode{intu, V, W, next;} NODE[MAXN];voidAdd_ (intUintVintW) {node[cnt].u=u; NODE[CNT].V=v; NODE[CNT].W=W; Node[cnt].next=Head[u]; Head[u]= cnt++;}voidAddintUintVintW)    {add_ (U, V, W); Add_ (V, U, W);}intSPFA (intSintt) {     for(inti =0; i < MAXN; i++) D[i] =INF; Queue<int>Q; Mem (Vis,0);    Q.push (s); Vis[s]=1; D[s]=0;  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) {D[E.V]= D[u] +E.W; if(!VIS[E.V])                    {Q.push (E.V); VIS[E.V]=1; }            }        }    }//cout << s << "" << t << Endl;//cout << d[t] << Endl;    returnd[t];}voidinit () {mem (head,-1); Mem (The,-1); CNT=0;}intMain () {init (); intEdge_sum =0; intu, V, W; CIN>> N >>m;  for(inti =0; I < m; i++) {cin>> u >> v >>W;        Add (U, V, W); Deg[u]++; DEG[V]++; //Way[u][v] = way[v][u] = w;Edge_sum + =W; }     for(inti =0; I < n; i++)if(Deg[i] &1) Odd.push_back (i); //the number of n is relatively small, so use a pressure DP to enumerate all cases    intLen =odd.size ();  for(inti =0; I < (1<< Len); i++) Dp[i] =INF; dp[0] =0;  for(intMask =0; Mask < (1<< Len); mask++)    {        intncnt = __builtin_popcount (mask);//How many 1 are there in the stats mask?        if(Ncnt &1)Continue; Vector<int> bits;//Bits[i] Represents the subscript bits[i] bit 1 is also the number of bits[i] in the odd         for(inti =0; i < Len; i++)            if(Mask & (1<<i)) bits.push_back (i); //int blen = Bits.size ();         for(inti =0; I < ncnt-1; i++)        {             for(intj = i +1; J < ncnt; J + +)            {                intSp_mask = Mask ^ (1<< Bits[i]) ^ (1<<Bits[j]); intU = odd[bits[i]], V =Odd[bits[j]]; intShost_path = way[u][v] = =-1?SPFA (U, v): Way[u][v]; WAY[U][V]= Way[v][u] =Shost_path; Dp[mask]= Min (Dp[mask], Dp[sp_mask] +Shost_path); } }} cout<< Edge_sum + dp[(1<< Len)-1] <<Endl; return 0;}

Chinese Postman Problem Aizu-dpl_2_b (No map China postman problem)

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.