二进制状态压缩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个城市一次且仅且一次,然后回到再回到出发点.问销售员应如何经过这些城市是他所走的路线最短? 用图论的语言描述就是:给 ...
随机推荐
- thikphp 简单的接口骨架
//get id 获取内容,调用xml方法 public function get(){ $id = $_GET['id'];//接收id $User = M('user'); //$val-> ...
- Linux ad7606 驱动
Linux中已经移植好了ad7606,位于driver/staging/iio/adc/目录中.只要在板级文件中添加device中即可. 移植参考文档: https://wiki.analog.com ...
- linux 打包 解压 tar zip tgz
.tar 解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)------------------------- ...
- php -- 取日期
1.获取当前时间方法date()很简单,这就是获取时间的方法, 格式为:date($format, $timestamp), format为格式 - 必需 timestamp为时间戳–可填参数. 比如 ...
- WF4.0——升级技能:泛型应用
前提: 在项目的开发中.我们知道,增加泛型,通过对类型的封装,进行抽象后.能够大大降低我们代码量,在项目中,泛型能够说是高级project师必备的技能之中的一个.也是面向对象的核心"抽象&q ...
- JBPM——工作流概念
一.概念 工作流(Workflow),就是"业务过程的部分或总体在计算机应用环境下的自己主动化",它主要解决的是"使在多个參与者之间依照某种提前定义的规 ...
- Oracle查询优化-多表查询
--合并结果集 --1.union all UNION ALL--单纯合并 ; --2.union UNION --将重复结果集合并 ; --------------使用命令窗口执行,查看union与 ...
- 演示-JQuery关系选择器
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- VR资源浏览网站
https://my.matterport.com 资源 https://my.matterport.com/show/?m=kCeVCzCjQ5s
- POJ 2923 Relocation(01背包变形, 状态压缩DP)
Q: 如何判断几件物品能否被 2 辆车一次拉走? A: DP 问题. 先 dp 求解第一辆车能够装下的最大的重量, 然后计算剩下的重量之和是否小于第二辆车的 capacity, 若小于, 这 OK. ...