Poj 2404 Jogging Trails (China postman problem, status compression DP)

Source: Internet
Author: User

Gord is preparing for a marathon. There is a park behind his home. There are many paths in the park that connect water spots (n <= 15 ). When Gord was training, she wanted to go through all the paths at least once and asked her how long it would take.

Question: Calculate the path between any two points, calculate the surprising vertex, and find the least weighted matching of these odd vertices (it seems that the optimal weighted matching in a general graph is not good, it is wrong to Split points and then use km ). An algorithm called Edmonds-Johnson can solve Chinese Postman problems, but I cannot find specific code.

#include <iostream>using namespace std;#define MAXN 30#define INF 999999999#define min(a,b) ( (a)<(b) ? (a):(b) )int deg[MAXN];int edge[MAXN][MAXN];int dp[1<<15];void Floyd ( int n ){for ( int k = 1; k <= n; k++ )for ( int i = 1; i <= n; i++ )if ( edge[i][k] != INF )    for ( int j = 1; j <= n; j++ )if ( edge[k][j] != INF )    edge[i][j] = min ( edge[i][j], edge[i][k] + edge[k][j] );}int dfs ( int st, int n ){if ( st == 0 ) return 0;if ( dp[st] > 0 ) return dp[st];dp[st] = INF;for ( int i = 1; i <= n; i++ ){if ( st & (1<<(i-1)) ){for ( int j = 1; j <= n; j++ )if ( i != j && (st & (1<<(j-1))) ){int tmp = dfs ( st^(1<<(i-1))^(1<<(j-1)), n ) + edge[i][j];if ( dp[st] > tmp ) dp[st] = tmp;}}}return dp[st];}int main(){int n, m, u, v, l, i, j;int res, st;while ( scanf("%d",&n) && n ){scanf("%d",&m);;for ( i = 1; i <= n; i++ )for ( j = 1; j <= n; j++ )edge[i][j] = INF;res = 0;memset(deg,0,sizeof(deg));while ( m-- ){scanf("%d%d%d",&u,&v,&l);if ( edge[u][v] > l )edge[u][v] = edge[v][u] = l;res += l; deg[u]++; deg[v]++;}Floyd ( n );for ( st = 0, i = 1; i <= n; i++ )if ( deg[i] % 2 == 1 )st |= (1<<(i-1));memset(dp,-1,sizeof(dp));res = res + dfs ( st, n );printf("%d\n",res);}return 0;}

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.