最小汉密尔顿回路问题 状态压缩dp
2015-05-01 21:07
411 查看
给定n个顶点做成的图,要求从顶点0出发经过所有点一次然后回到0点的一条权值之和最小的一条路的权值
#include <cstdio> #include <iostream> #include <algorithm> #include <queue> #include <stack> #include <climits> #include <cstring> #include <cmath> #include <map> #include <set> #define INF 100000000 using namespace std; struct node { int end; int state; }; int n,m; int ma[100][100]; int dp[100][1<<16]; int main(){ while(cin >> n >> m){ for(int i = 0;i < m;i++){ int x,y,w; cin >> x >> y >> w; ma[x][y] = w; ma[y][x] = w;//如果是有向图只要改下这里 } queue<node> que; node s; s.end = 0; s.state = 1; dp[s.end][s.state] = 0; que.push(s); while(!que.empty()){ s = que.front(); que.pop(); for(int i = 0;i < n;i++){ if(ma[s.end][i]){ if(!(s.state&(1 << (i)))){ //这个点之前没有走过 dp[i][s.state^(1 << (i))] = dp[s.end][s.state] + ma[s.end][i]; que.push(node{i,s.state^(1<<(i))}); } } } } int ans = INF; for(int i = 1;i < n;i++){ if(ma[i][0] && dp[i][(1<<n)-1]){ ans = min(ans,ma[i][0] + dp[i][(1<<n) - 1]); } } cout << ans << endl; } return 0; }
相关文章推荐
- hdu 1565 方格取数(1) (最小割/状态压缩+DP)
- POJ 1321 棋盘问题(状态压缩DP)
- Vijos 1456 最小总代价(状态压缩DP)
- 铺砖问题(状态压缩DP)
- 【上海交大oj】邮递员小F(状态压缩dp)(旅行商问题)
- (复习)(转)03进制状态压缩DP——HDU3001 Travelling 旅行商问题
- POJ 3311 旅行商问题 状态压缩 dp
- dp状态压缩-铺砖问题
- TSP 旅行商问题(状态压缩dp)
- dp状态压缩-铺砖问题
- 最短路+状态压缩dp(旅行商问题)hdu-4568-Hunter
- 最短路+状态压缩dp(旅行商问题)hdu-4568-Hunter
- 铺砖问题 (状态压缩DP)
- HDU4281:Judges' response (mTSP问题)(01背包 状态压缩 DP)
- poj1795(状态压缩dp,字符串压缩求最小字典序)
- 状态压缩dp问题
- Light OJ 1406 Assassin`s Creed 状态压缩DP+强连通缩点+最小路径覆盖
- POJ 1321 棋盘问题(状态压缩DP | DFS)
- POJ 3311 Hie with the Pie (状态压缩DP ,TSP问题)
- 状态压缩dp入门--------TSP问题