POJ 3311 Hie with the Pie - 状压dp【TSP】
来源:互联网 发布:手机监听监控定位软件 编辑:程序博客网 时间:2024/05/17 03:53
题目描述
题目大意:
一个人从固定出发点0,到一些城市(1~n)送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短。
注意:每一个城市可重复访问多次。两个城市间的直接可达路径不一定是最短路径。
求最短路径。
分析:
因为要遍历每个城市,再加上数据规模小,自然想到TSP经典状压dp问题。
但由于此题中每个城市可以访问多次,先Floyd预处理出每两个点之间的最短距离,就可以用TSP的方法做了。
#include<cstdio>#include<algorithm>#include<cstring>using namespace std;#define MAXN 10#define MAXST 2048#define INF 2000000000int n,dist[MAXN+10][MAXN+10],dp[MAXST+10][MAXN+10];void Init(){ memset(dp,-1,sizeof dp);}void read(){ for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) scanf("%d",&dist[i][j]);}int DP(int s,int u){ if(dp[s][u]!=-1) return dp[s][u]; int t=INF; for(int i=0;i<=n;i++){ if(!(s&(1<<i))) continue; t=min(t,DP(s^(1<<i),i)+dist[i][u]); } return dp[s][u]=t;}void Floyd(){ for(int k=0;k<=n;k++) for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);}int main(){ while(scanf("%d",&n)&&n){ Init(); read(); Floyd(); dp[0][0]=0; printf("%d\n",DP((1<<(n+1))-1,0)); }}
0 0
- POJ 3311--Hie with the Pie(状压dp+TSP)
- 【DP】 POJ 3311 Hie with the Pie 状压 TSP问题
- POJ 3311-Hie with the Pie(floyd+TSP 状压DP)
- POJ 3311 Hie with the Pie - 状压dp【TSP】
- poj 3311 Hie with the Pie(TSP ,状压dp)
- POJ 3311 Hie with the Pie -- TSP 状态压缩dp
- POJ 3311 Hie with the Pie(TSP问题DP解)
- poj 3311 Hie with the Pie (TSP)
- POJ 3311 Hie with Pie 状压dp,tsp
- HDU 3311 Hie with the Pie(状压dp+TSP问题)
- POJ 3311 Hie with the Pie (TSP问题 状压DP解法)
- POJ 3311 Hie with the Pie (Floyd + 状压dp 简单TSP问题)
- POJ 3311 Hie with the Pie(TSP问题 状压DP)
- poj 3311 Hie with the Pie (DP)
- POJ3311 Hie with the Pie 【TSP】【状压dp】
- poj 3311 Hie with the Pie (TSP问题)
- Poj 3311 Hie with the Pie 状态压缩(TSP)
- poj 3311 Hie with the Pie(TSP 状态压缩)
- MFC读取图片并显示
- 父母尚在苟且,你却在炫耀诗和远方
- YMS Round #1 Div. 2 B Angry Cows
- 基因表达式程序设计C++实现
- Node.js中的全局对象global(一)
- POJ 3311 Hie with the Pie - 状压dp【TSP】
- 第一篇
- Centos7 root密码重置
- HTML学习笔记
- Java,bit比特,byte字节,char字符,short,int,long,float,double,string,字母,汉字/编码等
- IOS 屏幕遮罩 Mask
- 线程干扰(Thread Interference)
- iOS Stroyboard找不到程序入口的设置方法
- Java代码优化(长期更新)