Light oj 1086 - Jogging Trails(状压dp 将图变成欧拉图)
2015-09-21 22:03
591 查看
1086 - Jogging Trails
Robin is training for a marathon. Behind his house is a park with a large network of jogging trails connecting water stations. Robin wants to find the shortest jogging route that travels along every trail at least once.
For each case, there should be one line of output giving the length of Robin's jogging route.
Each case contains two positive integers n (2 ≤ n ≤ 15), the number of water stations, and m (0 ≤ 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 travelled in either direction. It is possible to reach any trail from any other trail by visiting a sequence of water stations connected by trails. Robin's route may start at any water station, and must end
at the same station.
PROBLEM SETTER: GORDON V. CORMACK
SPECIAL THANKS: JANE ALAM JAN (SOLUTION, DATASET)
PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
For each case, there should be one line of output giving the length of Robin's jogging route.
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.Each case contains two positive integers n (2 ≤ n ≤ 15), the number of water stations, and m (0 ≤ 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 travelled in either direction. It is possible to reach any trail from any other trail by visiting a sequence of water stations connected by trails. Robin's route may start at any water station, and must end
at the same station.
Output
For each case, print the case number and the minimum possible length of Robin's jogging routeSample Input | Output for Sample Input |
1 4 5 1 2 3 2 3 4 3 4 5 1 4 10 1 3 12 | Case 1: 41 |
SPECIAL THANKS: JANE ALAM JAN (SOLUTION, DATASET)
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<stack> #include<vector> #include<set> #include<map> #define L(x) (x<<1) #define R(x) (x<<1|1) #define MID(x,y) ((x+y)>>1) #define bug printf("hihi\n") #define eps 1e-12 typedef long long ll; using namespace std; #define INF 0x3f3f3f3f #define N 16 int dp[1<<N]; int n,m; int a ; int in ; void floyd() { int i,j,k; for(int k=1;k<=n;k++) for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) a[i][j]=min(a[i][j],a[i][k]+a[k][j]); } void DP() { int i,j; floyd(); memset(dp,INF,sizeof(dp)); int len=1<<n; dp[len-1]=0; for(int cur=len-1;cur>=0;cur--) { for(int i=1;i<=n;i++) { if(!(cur&(1<<(i-1)))) continue; if(!(in[i]&1)) { int to=cur^(1<<(i-1)); dp[to]=min(dp[to],dp[cur]); continue; } for(int j=1;j<=n;j++) { if(in[j]%2==0) continue; if(i==j) continue; int to=cur^(1<<(i-1))^(1<<(j-1)); dp[to]=min(dp[to],dp[cur]+a[i][j]); } } } } int main() { int i,j,all,t,ca=0; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); memset(a,INF,sizeof(a)); int u,v,len; all=0; memset(in,0,sizeof(in)); while(m--) { scanf("%d%d%d",&u,&v,&len); if(len<a[u][v]) a[u][v]=a[v][u]=len; all+=len; in[u]++; in[v]++; } DP(); printf("Case %d: %d\n",++ca,dp[0]+all); } return 0; }
相关文章推荐
- Sublime Text 2 - There are no packages available for installation 解决方法(亲测可行)
- E: 无法获取锁 /var/lib/apt/lists/lock - open (11: Resource temporarily unavailable)解决方法
- exit()和_exit()函数 http://www.cnblogs.com/hnrainll/archive/2011/08/17/2142001.html
- Codeforces__Raising Bacteria
- Codeforces__Raising Bacteria
- xCode 7 编译出现错误:does not contain bitcode 解决办法
- 【转】友好的 ContainerView & AutoLayout 资料推荐
- [置顶] Linux DM9000网卡驱动程序完全分析 http://blog.csdn.net/ypoflyer/article/details/6209922
- AI-Tank
- 查询表锁争用情况 table_locks_waited & table_locks_immedaite
- setContentView(R.Layout.main)的意思
- raid5实现原理
- RAID5服务器磁盘阵列,两块盘坏了数据恢复
- http://blog.csdn.net/abcjennifer/article/details/7716281 过拟合问题
- 为什么使用HttpUrlConnection时候,用available方法不准确
- failed.org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hiber
- shell浅谈之九子shell与进程处理 http://blog.csdn.net/taiyang1987912/article/details/39529291
- 23种设计模式之一(创建型模式)Factory模式 http://blog.csdn.net/taiyang1987912/article/details/43148913
- 磁盘I/O性能----RAID
- servlet中图像上传以及缩小