二进制状态压缩dp(旅行商TSP)POJ3311
http://poj.org/problem?id=3311
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 4456 | Accepted: 2355 |
Description
The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can afford to hire only one driver to do the deliveries. He will wait for 1 or more (up to 10) orders to be processed before
he starts any deliveries. Needless to say, he would like to take the shortest route in delivering these goodies and returning to the pizzeria, even if it means passing the same location(s) or the pizzeria more than once on the way. He has commissioned you
to write a program to help him.
Input
Input will consist of multiple test cases. The first line will contain a single integer n indicating the number of orders to deliver, where 1 ≤ n ≤ 10. After this will be n + 1 lines each containing n + 1 integers indicating
the times to travel between the pizzeria (numbered 0) and the n locations (numbers 1 to n). The jth value on the ith line indicates the time to go directly from location i to location j without visiting
any other locations along the way. Note that there may be quicker ways to go from i to j via other locations, due to different speed limits, traffic lights, etc. Also, the time values may not be symmetric, i.e., the time to go directly from
location i to j may not be the same as the time to go directly from location j to i. An input value of n = 0 will terminate input.
Output
For each test case, you should output a single number indicating the minimum time to deliver all of the pizzas and return to the pizzeria.
Sample Input
3
0 1 10 10
1 0 1 2
10 1 0 10
10 2 10 0
0
Sample Output
8
题意:从0出发,要求经过1-n的所有点,可能会经过多次;问最后回到0点的最少时间是多少?
分析:首先用floyd求出两两点之间的最短有向路,这就抽象的形成了每个点只经过一次的旅行商问题,用状态压缩枚举每种状态,初始化的时候可以从1-n的任意点出发;dp[1<<i][i]=dis[0][i];代表已经经过了0,所以最后回到0 的时候0也是经过了一次;
#include"stdio.h"
#include"string.h"
#include"iostream"
#include"map"
#include"string"
#include"queue"
#include"stdlib.h"
#include"algorithm"
#include"math.h"
#define M 60001
#define eps 1e-10
#define inf 100000000
#define mod 100000000
#define INF 0x3f3f3f3f
using namespace std;
int dp[1<<13][13],px[13];
int G[13][13],dis[13][13];
void floyd(int n)
{
int i,j,k;
for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
dis[i][j]=G[i][j];
for(k=0;k<=n;k++)
{
for(i=0;i<=n;i++)
{
for(j=0;j<=n;j++)
{
if(dis[i][k]>=INF||dis[k][j]>=INF)continue;
if(dis[i][j]>dis[i][k]+dis[k][j])
dis[i][j]=dis[i][k]+dis[k][j];
}
}
}
}
int main()
{
int i,j,k,n;
px[0]=1;
for(i=1;i<=11;i++)
px[i]=px[i-1]*2;
while(scanf("%d",&n),n)
{
for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
scanf("%d",&G[i][j]);
floyd(n);
memset(dp,INF,sizeof(dp));
for(i=1;i<=n;i++)
dp[1<<i][i]=dis[0][i];//从第i个点开始已经走了多远;
for(i=1;i<px[n+1];i++)
{
for(j=0;j<=n;j++)
{
int tep=i&(1<<j);
if(tep==0)continue;
int cur=i^(1<<j);
for(k=0;k<=n;k++)
{
if(dp[cur][k]>=INF)continue;
if(k==j)continue;
tep=cur&(1<<k);
if(tep==0)continue;
if(dp[i][j]>dp[cur][k]+dis[k][j])
dp[i][j]=dp[cur][k]+dis[k][j];
}
}
}
printf("%d\n",dp[px[n+1]-1][0]);
}
}
二进制状态压缩dp(旅行商TSP)POJ3311的更多相关文章
- BFS+状态压缩DP+二分枚举+TSP
http://acm.hdu.edu.cn/showproblem.php?pid=3681 Prison Break Time Limit: 5000/2000 MS (Java/Others) ...
- 三进制状态压缩DP(旅行商问题TSP)HDU3001
http://acm.hdu.edu.cn/showproblem.php?pid=3001 Travelling Time Limit: 6000/3000 MS (Java/Others) ...
- TSP 旅行商问题(状态压缩dp)
题意:有n个城市,有p条单向路径,连通n个城市,旅行商从0城市开始旅行,那么旅行完所有城市再次回到城市0至少需要旅行多长的路程. 思路:n较小的情况下可以使用状态压缩dp,设集合S代表还未经过的城市的 ...
- BZOJ1688|二进制枚举子集| 状态压缩DP
Disease Manangement 疾病管理 Description Alas! A set of D (1 <= D <= 15) diseases (numbered 1..D) ...
- HOJ 2226&POJ2688 Cleaning Robot(BFS+TSP(状态压缩DP))
Cleaning Robot Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4264 Accepted: 1713 Descri ...
- [poj3311]Hie with the Pie(Floyd+状态压缩DP)
题意:tsp问题,经过图中所有的点并回到原点的最短距离. 解题关键:floyd+状态压缩dp,注意floyd时k必须在最外层 转移方程:$dp[S][i] = \min (dp[S \wedge (1 ...
- POJ 3311 Hie with the Pie(Floyd+状态压缩DP)
题是看了这位的博客之后理解的,只不过我是又加了点简单的注释. 链接:http://blog.csdn.net/chinaczy/article/details/5890768 我还加了一些注释代码,对 ...
- 状态压缩DP(大佬写的很好,转来看)
奉上大佬博客 https://blog.csdn.net/accry/article/details/6607703 动态规划本来就很抽象,状态的设定和状态的转移都不好把握,而状态压缩的动态规划解决的 ...
- 旅行商问题——状态压缩DP
问题简介 有n个城市,每个城市间均有道路,一个推销员要从某个城市出发,到其余的n-1个城市一次且仅且一次,然后回到再回到出发点.问销售员应如何经过这些城市是他所走的路线最短? 用图论的语言描述就是:给 ...
随机推荐
- SparkR:数据科学家的新利器
摘要:R是数据科学家中最流行的编程语言和环境之一,在Spark中加入对R的支持是社区中较受关注的话题.作为增强Spark对数据科学家群体吸引力的最新举措,最近发布的Spark 1.4版本在现有的Sca ...
- windows下定时任务设置
Linux 系统可以通过crontab -e 设置定时任务,Windows系统没有crontab命令,但是Windows系统有跟crontab命令比较接近的命令: schtasks 命令. # 设置定 ...
- QTableWidget的表头颜色设置
设置水平和垂直表头的颜色ui->tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{b ...
- mysql的优化:官网地址
http://dev.mysql.com/doc/refman/5.1/zh/optimization.html#index-merge-optimization
- Java回调方法的设计思路
package com.test; /** * 回调方法的设计技巧,例如hibernate的getHibernateTemplate().execute(Handler h)方法 */ public ...
- 【Java面试题】34 List 、Map、Set 区别?
一.Set是最简单的一种集合.集合中的对象不按特定的方式排序,并且没有重复对象. Set接口主要实现了两个实现类: HashSet: HashSet类按照哈希算法来存取集合中的对象,存取速度比较快 T ...
- 【Java 线程的深入研究4】ThreadPoolExecutor运转机制详解
hreadPoolExecutor机制 一.概述 1.ThreadPoolExecutor作为java.util.concurrent包对外提供基础实现,以内部线程池的形式对外提供管理任务执行,线程调 ...
- 感谢各位亲们的大力支持,免费的HTML5学习课程《HTML5网页开发实例具体解释》连载已经结束了!
感谢各位亲们的大力支持,免费的HTML5学习课程<HTML5网页开发实例具体解释>连载已经结束了. 有兴趣的读者能够看我的博客,也能够看以下的链接逐个学习: 当里个当.免费的HTML5连 ...
- Davlik虚拟机
过几天得去面试,感觉原来做的东西都忘了. 有点累,无意看了下二师兄的小论文,想来原先自己也参与过一点点,所以记录下: Dalvik虚拟机中共有3种解释器,分别时SWITCH_INTERP,THREAD ...
- VS2010属性表的建立与灵活运用
问题引入:在VS2010当中,进行opencv.QT等的编程时,总是需要配置很多属性还有依赖项等,为了减少每次都重复配置属性的工作量,现在可以运行属性表这个东西来简化配置.opencv也可以这样建立使 ...