POJ3311 Hie with the Pie(状态压缩dp)
来源:互联网 发布:数控车床g73编程实例 编辑:程序博客网 时间:2024/04/30 02:03
题目要求,从0出发,必须经过N个点,然后回到0点。
每两个点之间花费的时间不同,i到j和j到i也不同,求最短时间。
1先用floyd算法求出两点之间最短路径
2递归方程 dp[j][i]=min(dp[j][i],dp[k][i&(~(1<<j))]+map[k][j]);
j表示终点 i为二进制表示状态 1表示经过 0表示不经过 把所有可能的路径排列出来 找最小值
i&(~(1<<j)) 表示i的第j位从1变成0之后的二进制数
k表示中转点 先从去掉一个经过点的位置到k 再从k到j 看这样的路径会不会比i的情况短
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int n,map[12][12],dp[12][1<<12];void floyd()//找一个矩阵 两点之间的最短路径{ int i,j,k; for(k=0;k<=n;k++) for(i=0;i<=n;i++) for(j=0;j<=n;j++) map[i][j]=min(map[i][k]+map[k][j],map[i][j]);}int main(){ int i,j,k,m; scanf("%d",&n); while(n) { memset(dp,0x3f,sizeof(dp));//记住最大值的表示啊。。0x3f 0x3f 0x3f for(i=0;i<=n;i++) for(j=0;j<=n;j++) scanf("%d",&map[i][j]); floyd(); for(i=0;i<=n;i++)//把dp[i][1<<i](只用通过一个点的情况)初始化为0到i的距离 dp[i][1<<i]=map[0][i]; m=(1<<(n+1)); for(i=0;i<m;i++)//总状态数 for(j=0;j<=n;j++)//j为终点 递归 for(k=0;k<=n;k++)//k是取一个中间点 { if((i>>j)%2)//先判断下第j位是不是1 是的话 就把这一点当做中间点 看总路程会不会小一些 dp[j][i]=min(dp[j][i],dp[k][i&(~(1<<j))]+map[k][j]); } //map[][]前面是起点 后面是终点 dp[][]反过来 别弄反了 printf("%d\n",dp[0][m-1]); scanf("%d",&n); } return 0;}
- POJ3311 Hie with the Pie(状态压缩dp)
- poj3311 Hie with the Pie(状态压缩dp)
- POJ3311:Hie with the Pie(floyd+状态压缩DP)
- 【POJ3311】Hie with the Pie(状态压缩DP)
- [Floyd+状态压缩DP]poj3311 Hie with the Pie
- POJ3311:Hie with the Pie(floyd+状态压缩DP)
- poj3311 Hie with the Pie,状态压缩
- POJ3311——Hie with the Pie(状态压缩DP)
- POJ3311——Hie with the Pie(floyd,状态压缩dp,旅行商)
- poj3311 Hie with the Pie【floyd最短路+状态压缩】
- poj3311 Hie with the Pie(最短路+状态dp)
- poj3311(Hie with the Pie)状压dp
- POJ3311 Hie with the Pie 状压DP
- [POJ3311] Hie with the Pie && 状压DP
- [ 状压DP][POJ3311] Hie with the Pie
- POJ 3311 Hie with the Pie(状态压缩DP)
- POJ 3311 Hie with the Pie(状态压缩DP)
- POJ 3311 Hie with the Pie(状态压缩dp)
- js获取Url,域名,端口号等
- Android 进度条的使用
- 【转】C#: 带有背景文本的TextBox
- 图的邻接表实现迪杰斯特拉算法求最短路径
- Storm集群安装部署步骤【详细版】
- POJ3311 Hie with the Pie(状态压缩dp)
- android开发 java.lang.VerifyError:错误
- struts2的验证码及利用jquery发送ajax请求并利用json做数据交换
- 设计模式之策略模式(Strategy)
- 解决Fedora登陆时笔记本上亮度最低
- linq To DataTable 实现Group by 多个栏位
- GoAsm [command line switches] filename[.ext]
- 报错Cannot set permission for /hdata/hdfs/system. Name node is in safe mode.
- StrutsPrepareAndExecuteFilter与FilterDispatcher的区别