Jogging Trails
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:2122 |
|
Accepted:849 |
Description
Gord is training for a marathon. behind his house is a park with a large network of Jogging Trails connecting water stations. gord wants to find the shortest jogging route that travels along every trail at least once.
Input
Input consists of several test cases. the first line of input for each case contains two positive integers: n <= 15, the number of water stations, and m <1000, the number of trails. for each trail, there is one subsequent line of input containing three positive integers: the first two, between 1 and N, indicating the water stations at the end points of the trail; the third indicates the length of the trail, in cubits. there may be more than one trail between any two stations; each different trail is given only once in the input; each trail can be traveled in either direction. it is possible to reach any trail from any other trail by visiting a sequence of water stations connected by trails. gord's route may start at any water station, and must end at the same station. A single line containing 0 follows the last test case.
Output
For each case, there shocould be one line of output giving the length of Gord's jogging route.
Sample Input
4 51 2 32 3 43 4 51 4 101 3 120
Sample output
41
Source
Waterloo local 2002.07.01
Only the problem-solving report I can see that some people say that km can solve the problem. The actual experiment cannot be done. The cause of the error is that after we split the points, there will certainly be symmetric edges, we hope that the biggest matching edge will also have a pair of symmetry, so that the final result can be divided by 2, but actually the matching edge may not be symmetric, leading to an error.
Http://www.cnblogs.com/wuminye/archive/2013/05/06/3063902.html
This person wrote a good blog and can learn from it.
#include <iostream>#include <cstring>#include <cstdio>#include <cmath>#include <algorithm>#include <cstdlib>#include <queue>#define N 20#define M 1000000#define INF 0x7fffffusing namespace std;int a[N][N],d[N],dis[M];bool inque[M];int n,m;int main(){ //freopen("data.txt","r",stdin); int bfs(int x); while(scanf("%d",&n)!=EOF) { if(n==0) { break; } scanf("%d",&m); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { a[i][j] = INF; } } int res = 0; memset(d,0,sizeof(d)); for(int i=1;i<=m;i++) { int x,y,val; scanf("%d %d %d",&x,&y,&val); d[x]++; d[y]++; res+=val; a[x][y] = min(a[x][y],val); a[y][x] = min(a[y][x],val); } for(int k=1;k<=n;k++) { for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(i==k||i==j||k==j) { continue; } a[i][j] = min(a[i][j],a[i][k]+a[k][j]); } } } int sum=0,db=1; for(int i=1;i<=n;i++) { if(d[i]%2) { sum+=db; } db = db*2; } int ans = bfs(sum); printf("%d\n",ans+res); } return 0;}int bfs(int x){ memset(inque,false,sizeof(inque)); for(int i=0;i<=((1<<n)-1);i++) { dis[i] = INF; } queue<int>que; que.push(x); inque[x] = true; dis[x] = 0; int op[20],op2[20]; op2[0] = 1; for(int i=1;i<=n;i++) { op2[i] = op2[i-1]*2; } while(!que.empty()) { x = que.front(); que.pop(); inque[x] = false; int xx = x; for(int i=1;i<=n;i++) { op[i] = xx%2; xx = xx/2; } for(int i=1;i<=n;i++) { if(op[i]) { for(int j=i+1;j<=n;j++) { if(op[j]) { int y =x-op2[i-1]-op2[j-1]; if(dis[y]>dis[x]+a[i][j]) { dis[y] = dis[x]+a[i][j]; if(!inque[y]) { que.push(y); inque[y] = true; } } } } } } } return dis[0];}