POJ 3311 Hie with the Pie Floyd+状压dp
来源:互联网 发布:非诚勿扰网络直播时间 编辑:程序博客网 时间:2024/06/07 05:19
题目链接:
https://vjudge.net/contest/159644#problem/C
题意:
有N个城市(1~N)和一个PIZZA店(0),要求一条回路,从0出发,又回到0,而且距离最短
题解:
TSP问题: 挑战上面P193
首先floyd求出两点之间的最短路
然后状压, 对于状态S,第i位为1表示已经走过
dp[S][v]:=当前状态为S,在v这个点上,访问剩余没有访问的所有点(S为0的位)的最短距离
我们逆推,初态:dp[(1<<(n+1))-1][0] = 0; 也就是所有点都访问过了,在0这个点上,最短距离就是0。最终的答案是dp[0][0],当没有访问任何点,在0这个点上的最短距离。
转移:dp[S][v] = min(dp[S][v],dp[S|(1<<u)][u]+dis[v][u]); // v->u, v是u的上一个点
当然也可以正推:
定义状态DP(S,i)表示在S状态下,到达城市I的最优值
接着状态转移方程:
DP(S,i)=min{DP(S^(1<<i-1),k) + dis[k][i],DP(S,i)}// 其中S^(1<<i-1)表示未到达城市i的所有状态,1<=k<=n
http://blog.csdn.net/chinaczy/article/details/5890768
代码:
#include <cstdio>#include <iostream>#include <cstring>using namespace std;typedef long long ll;#define MS(a) memset(a,0,sizeof(a))#define MP make_pair#define PB push_backconst int INF = 0x3f3f3f3f;const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;inline ll read(){ ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}//////////////////////////////////////////////////////////////////////////const int maxn = 1e5+10;int dp[(1<<11)][11],dis[15][15];int main(){ int n; while(cin>>n && n){ for(int i=0; i<=n; i++){ for(int j=0; j<=n; j++){ cin >> dis[i][j]; } } for(int k=0; k<=n; k++) for(int i=0; i<=n; i++) for(int j=0; j<=n; j++) if(i!=j && (i!=k || k!=j)) dis[i][j] = min(dis[i][j],dis[i][k]+dis[k][j]); // dp[S][v]:=当前状态为S,在v这个点上,访问剩余没有访问的所有点(S为0的位)的最短距离 memset(dp,INF,sizeof(dp)); dp[(1<<(n+1))-1][0] = 0; // 逆推,初态:dp[(1<<(n+1))-1][0] = 0; 也就是所有点都访问过了,在0这个点上,最短距离就是0 for(int S = (1<<(n+1))-2; S>=0; S--){ for(int v=0; v<=n; v++) for(int u=0; u<=n; u++){ if(!((S>>u)&1)){ dp[S][v] = min(dp[S][v],dp[S|(1<<u)][u]+dis[v][u]); // v->u, v是u的上一个点 } } } cout << dp[0][0] << endl; } return 0;}
0 0
- poj 3311 Hie with the Pie(floyd + 状压dp)
- POJ 3311 Hie with the Pie floyd+状压DP
- POJ 3311 Hie with the Pie(状压DP + Floyd)
- POJ 3311-Hie with the Pie(floyd+TSP 状压DP)
- POJ 3311 Hie with the Pie(状压dp+floyd)
- poj 3311 Hie with the Pie(Floyd+状压dp)
- Poj 3311 Hie with the Pie【Floyd+状压dp】
- [POJ 3311]Hie with the Pie(状压DP+Floyd)
- POJ 3311 Hie with the Pie Floyd+状压dp
- POJ 3311 Hie with the Pie(Floyd + 状压dp)
- POJ 3311 Hie with the Pie (Floyd+状压DP)
- POJ 3311 Hie with the Pie(Floyd+状态压缩DP)
- POJ 3311 Hie with the Pie(Floyd+状态压缩DP)
- POJ 3311 Hie with the Pie(Floyd+状态压缩DP)
- poj 3311 Hie with the Pie floyd+状态压缩dp
- POJ 3311 Hie with the Pie(Floyd+状态压缩DP)
- poj 3311 Hie with the Pie(状态压缩DP+floyd)
- POJ 3311 Hie with the Pie 状态压缩DP+floyd
- 二叉树的所有路径
- Servlet3.0新特性(从注解配置到websocket编程)
- python操作数据库相关的代码
- LeetCode31. Next Permutation
- 基于C语言Win32API的可视化界面的qaac
- POJ 3311 Hie with the Pie Floyd+状压dp
- Android跑Monkey
- 记录自已学习之宏定义
- 二叉树的路径和
- Java结构型设计模式-装饰(二)
- 手机键盘
- 继承
- MFC多线程学习
- linux 新用户设置