POJ 3311 Hie with the Pie(状压DP)
来源:互联网 发布:ubuntu下好用的输入法 编辑:程序博客网 时间:2024/05/22 06:35
思路:一个类似TSP的问题,只是每个点可以走多次,floyd预处理一下两两之间的距离。求最短距离。城市只有10个,所以可以考虑状态压缩,令dp[s][i]为到了i点时状态为s的最短距离,那么dp[s][i]=min(dp[s][i],dp[ss][j]+d[j][i])
#include<iostream>#include<cstdio>#include<cstring>using namespace std;#define inf 1e9int dp[1<<11][11];int d[11][11];int main(){ int n;while(scanf("%d",&n)!=EOF && n){memset(dp,0,sizeof(dp));for(int i = 0;i<=n;i++)for(int j= 0;j<=n;j++)scanf("%d",&d[i][j]);for(int k = 0;k<=n;k++)for(int i = 0;i<=n;i++)for(int j = 0;j<=n;j++)d[i][j]=min(d[i][j],d[i][k]+d[k][j]);for(int s=0;s<(1<<n);s++){for(int i = 1;i<=n;i++) {if(s&(1<<(i-1))){if(s==(1<<(i-1)))dp[s][i]=d[0][i];else{dp[s][i]=inf;for(int j = 1;j<=n;j++){if(s&(1<<(j-1)) && j!=i){ dp[s][i]=min(dp[s][i],dp[s^(1<<(i-1))][j]+d[j][i]);}}}}}}int ans = dp[(1<<n)-1][1]+d[1][0];for(int i = 2;i<=n;i++)ans = min(ans,dp[(1<<n)-1][i]+d[i][0]);printf("%d\n",ans);}}
0 0
- POJ 3311--Hie with the Pie(状压dp+TSP)
- poj 3311 Hie with the Pie(floyd + 状压dp)
- POJ 3311 Hie with the Pie(状压DP + Floyd)
- POJ 3311 Hie with the Pie (状压DP)
- POJ 3311 Hie with the Pie(状压dp+floyd)
- poj 3311 Hie with the Pie(Floyd+状压dp)
- POJ 3311 Hie with the Pie (状压DP)
- POJ 3311 Hie with the Pie(状压DP)
- [POJ 3311]Hie with the Pie(状压DP+Floyd)
- POJ 3311 Hie with the Pie(Floyd + 状压dp)
- POJ 3311 Hie with the Pie(状压DP)
- poj 3311 Hie with the Pie(TSP ,状压dp)
- poj 3311 Hie with the Pie (DP)
- poj 3311 Hie with the Pie(状压dp)
- Hie with the Pie - POJ 3311 状压dp
- POJ 3311 Hie with the Pie (状压DP)
- POJ 3311 Hie with the Pie (状压DP)
- POJ 3311 Hie with the Pie floyd+状压DP
- ListView加载两种Item样式问题总结
- ionic之AngularJS扩展动态组件
- 学Android开发 这19个开发工具助你顺风顺水
- ERROR : arm-linux-ld:u-boot.lds:1: ignoring invalid character `#' in expression
- java创建线程
- POJ 3311 Hie with the Pie(状压DP)
- Android-->获取所有联系人以及所有的字段(联系人头像,邮箱,地址,公司等)
- Java7并发编程--3.2、CountDownLatch等待多个并发事件完成
- ipv6怎么转换成数字(bigInteger) ipv6 to BigInteger
- React-Native 学习系列课程笔记(React-Native开发环境搭建)
- Java7并发编程--3.3、CyclicBarrier在集合点同步
- 深入理解SQL的四种连接-左外连接、右外连接、内连接、全连接
- WEB-INF 有关的目录路径问题总结
- 使用Python调整字幕文件的时间轨