POJ 3311 Hie with the Pie(经典TSP问题)
来源:互联网 发布:文字抓取软件 编辑:程序博客网 时间:2024/05/17 02:25
题意:一个人从0送外卖,每次送外卖不超过10个地方,给你两两之间所需时间,求送完外卖回到店里的总时间最小,每个地方可以经历到任意次。
思路:弗洛伊德处理下就好了,然后就是经典tsp了
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;const int maxn = 12;const int INF = 0x3f3f3f3f;int dp[maxn][1<<maxn], dis[maxn][maxn], n;int main(){ while(~scanf("%d", &n), n) { int x; for(int i = 0; i <= n; i++) for(int j = 0; j <= n; j++) { scanf("%d", &x); dis[i][j] = x; } for(int k = 0; k <= n; k++) //任意次数就直接弗洛伊德 for(int i = 0; i <= n; i++) for(int j = 0; j <= n; j++) dis[i][j] = min(dis[i][j], dis[i][k]+dis[k][j]); memset(dp, INF, sizeof(dp)); dp[0][0] = 0; //能回到原点 0不要标记掉 int tot = (1<<(n+1))-1; for(int state = 0; state <= tot; state++) { for(int i = 0; i <= n; i++) //先枚举中间点 { if(dp[i][state] != INF) //如果这个状态算过才能算下面的最优 { //就算最短路经过了其他没标记过的点, 最后每个状态都会经历到,肯定会被更新成最优 for(int k = 0; k <= n; k++) { if(state&(1<<k)) continue; dp[k][state|(1<<k)] = min(dp[k][state|(1<<k)], dp[i][state]+dis[i][k]); } } } }// int ans = INF;// for(int i = 1; i <= n; i++)// ans = min(ans, dp[i][tot-1]+dis[i][0]); printf("%d\n", dp[0][tot]); } return 0;}
阅读全文
0 0
- POJ 3311 Hie with the Pie(经典TSP问题)
- POJ 3311 Hie with the Pie(经典TSP问题)
- poj 3311 Hie with the Pie (TSP)
- poj 3311 Hie with the Pie (TSP问题)
- 【DP】 POJ 3311 Hie with the Pie 状压 TSP问题
- POJ 3311 Hie with the Pie(TSP问题DP解)
- POJ 3311 Hie with the Pie TSP问题 -
- POJ 3311--Hie with the Pie(状压dp+TSP)
- Poj 3311 Hie with the Pie 状态压缩(TSP)
- poj 3311 Hie with the Pie(TSP ,状压dp)
- Pku oj 3311 Hie with the Pie(TSP问题)
- POJ 3311 Hie with the Pie(TSP问题 状压DP)
- POJ 3311 Hie with the Pie (状态压缩DP ,tsp问题)
- POJ 3311 Hie with the Pie (状态压缩DP ,TSP问题)
- POJ 3311 Hie with the Pie -- 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】
- 架构师必看书籍
- js内存泄漏
- R语言中之分布函数
- DQL 数据查询语句
- 聚类(matlab实践)
- POJ 3311 Hie with the Pie(经典TSP问题)
- kbe之ubuntu下的编译
- VBA处理文件框架代码 【第二部分:变量定义】
- header函数 自动下载文件
- intellij配置maven+SSM+redis(一)
- celery笔记
- java 集合排序
- Python使用dir获取类的方法列表
- (ZOJ) 3334 二分+贪心(二分double的写法)