Chinese Postman Problem CPP

Source: Internet
Author: User


Problem description: Chinese poster-man problem, CPP for short, provides a connected graph and asks the minimum path to be taken after each edge at least once with the same start point and end point.

Undirected graph CPP

1. Consider that when all vertices are even numbers, this figure is the European Union, so any Euler's circuit is the answer.

2. when two vertices are odd, you only need to find the shortest path between the two points and add the edges in the shortest path to the source image, this is to obtain an oocl (poj 1237 The Postal Worker Rings Once)

3. when the number of odd degree points is greater than 2, the degrees of each point must be changed to an even number, that is, n links are connected between 2n points, therefore, the problem is changed to the minimum matching problem of undirected graphs (the KM algorithm is incorrect because the solution cannot be guaranteed, and the flowering algorithm with flowers is correct). Generally, the problem is solved by pressing the dp. (Poj 2404 Jogging Trails)

For each odd degree, it is represented by a binary bit. 1 indicates that no matching is performed, 0 indicates that the matching is performed, and the status is updated when two matching points are not matched in the current status, then use the Memory search to find the optimal solution.

import java.io.IOException;import java.util.Arrays;import java.util.Scanner;public class Main {int dgr[] = new int[20];int map[][] = new int[20][20], inf = 1 << 28;Scanner scan = new Scanner(System.in);void floyd(int n) {for (int k = 1; k <=n; k++)for (int i = 1; i <=n; i++)for (int j = 1; j <=n; j++)map[i][j] = Math.min(map[i][j], map[i][k] + map[k][j]);}int stack[]=new int[20],top;int dp[]=new int[1<<17];int dfs(int s){if(s==0)return 0;if(dp[s]!=-1)return dp[s];int res=inf,i=0;while((s&(1<<i))==0) i++;for(int j=i+1;j<top;j++)if((s&(1<<j))!=0){int temp=dfs(s-(1<<i)-(1<<j))+map[stack[i]][stack[j]];res=Math.min(res, temp);}return dp[s]=res;}void run() {while (true) {int n = scan.nextInt();if (n == 0)break;int m = scan.nextInt();Arrays.fill(dgr, 0);for (int i = 0; i < 20; i++)Arrays.fill(map[i], inf);int sum = 0;while (m-- > 0) {int a = scan.nextInt();int b = scan.nextInt();int c = scan.nextInt();map[b][a]=map[a][b] = Math.min(map[a][b], c);sum += c;dgr[a]++;dgr[b]++;}floyd(n);top=0;for(int i=1;i<=n;i++)if(dgr[i]%2==1)stack[top++]=i;Arrays.fill(dp, -1);sum+=dfs((1<<top)-1);System.out.println(sum);}}public static void main(String[] args) throws IOException {new Main().run();}}

Directed Graph CPP:

Like an undirected graph, we try to construct an Euler's path with the minimum cost. First, we calculate the difference d (v) between the outbound and inbound degrees of each vertex, and then use the minimum cost flow to solve the problem, the diagram is as follows:

1. Its vertex set is all vertices of graph G, as well as additional super source s and super sink t;
2 for each edge (u, v) in graph G, the capacity of the edge (u, v) in N is inf, and the cost is the length of the edge;
3 from the Source Vertex s to all vertices of d (v) <0 v edge (s, v), capacity is-d (v), cost is 0;
4 From all vertices of d (v)> 0, u is directed to t edge (u, t). The capacity is d (v) and the cost is 0.

Hybrid image CPP:

If two-way access is allowed in some streets, only one-way access is allowed in some streets. This problem has been proved to be NPC.


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.