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;}