Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2122 | Accepted: 849 |
Description
Input
Output
Sample Input
4 5 1 2 3 2 3 4 3 4 5 1 4 10 1 3 12 0
Sample Output
41
Source
#include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <algorithm> #include <cstdlib> #include <queue> #define N 20 #define M 1000000 #define INF 0x7fffff using 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]; }
POJ 2404 Jogging Trails,布布扣,bubuko.com
原文:http://blog.csdn.net/yongxingao/article/details/37569719