http://poj.org/problem?id=3311

Hie with the Pie
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的更多相关文章

  1. BFS+状态压缩DP+二分枚举+TSP

    http://acm.hdu.edu.cn/showproblem.php?pid=3681 Prison Break Time Limit: 5000/2000 MS (Java/Others)   ...

  2. 三进制状态压缩DP(旅行商问题TSP)HDU3001

    http://acm.hdu.edu.cn/showproblem.php?pid=3001 Travelling Time Limit: 6000/3000 MS (Java/Others)     ...

  3. TSP 旅行商问题(状态压缩dp)

    题意:有n个城市,有p条单向路径,连通n个城市,旅行商从0城市开始旅行,那么旅行完所有城市再次回到城市0至少需要旅行多长的路程. 思路:n较小的情况下可以使用状态压缩dp,设集合S代表还未经过的城市的 ...

  4. BZOJ1688|二进制枚举子集| 状态压缩DP

    Disease Manangement 疾病管理 Description Alas! A set of D (1 <= D <= 15) diseases (numbered 1..D) ...

  5. HOJ 2226&POJ2688 Cleaning Robot(BFS+TSP(状态压缩DP))

    Cleaning Robot Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4264 Accepted: 1713 Descri ...

  6. [poj3311]Hie with the Pie(Floyd+状态压缩DP)

    题意:tsp问题,经过图中所有的点并回到原点的最短距离. 解题关键:floyd+状态压缩dp,注意floyd时k必须在最外层 转移方程:$dp[S][i] = \min (dp[S \wedge (1 ...

  7. POJ 3311 Hie with the Pie(Floyd+状态压缩DP)

    题是看了这位的博客之后理解的,只不过我是又加了点简单的注释. 链接:http://blog.csdn.net/chinaczy/article/details/5890768 我还加了一些注释代码,对 ...

  8. 状态压缩DP(大佬写的很好,转来看)

    奉上大佬博客 https://blog.csdn.net/accry/article/details/6607703 动态规划本来就很抽象,状态的设定和状态的转移都不好把握,而状态压缩的动态规划解决的 ...

  9. 旅行商问题——状态压缩DP

    问题简介 有n个城市,每个城市间均有道路,一个推销员要从某个城市出发,到其余的n-1个城市一次且仅且一次,然后回到再回到出发点.问销售员应如何经过这些城市是他所走的路线最短? 用图论的语言描述就是:给 ...

随机推荐

  1. D方法 自动完成

    控制器 public function insert(){ $Wztj = D("Wztj");if($vo=$Wztj->create()){ if($Wztj->a ...

  2. 关于Cocos2d-x中多边形物理刚体的设置

    1.如果想要设置某个物体有多边形的刚体,这样可以更精确地进行碰撞检测,可以用以下的方法 auto hero = PlaneHero::create(); addChild(hero, 0, HERO_ ...

  3. android LayoutInflater 笔记

    LayoutInflater类用于查找布局文件并实例化.用于动态将布局加入界面中. 参考链接 http://blog.csdn.net/guolin_blog/article/details/1292 ...

  4. yasm开源汇编器分析

    https://www.google.com.hk/search?q=yasm&oq=yasm&aqs=chrome..69i57&sourceid=chrome&es ...

  5. 常用CSS缩写语法总结(转)

    使用缩写可以帮助减少你CSS文件的大小,更加容易阅读.css缩写的主要规则如下: 颜色 16进制的色彩值,如果每两位的值相同,可以缩写一半,例如:#000000可以缩写为#000;#336699可以缩 ...

  6. R语言低级绘图函数-abline

    abline 函数的作用是在一张图表上添加直线, 可以是一条斜线,通过x或y轴的交点和斜率来确定位置:也可以是一条水平或者垂直的线,只需要指定与x轴或y轴交点的位置就可以了 常见用法: 1)添加直线 ...

  7. maven 打包可执行jar的两种方法

    1.修改pom.xml增加如下内容 <build> <pluginManagement> <plugins> <plugin> <groupId& ...

  8. u3d发布成全屏的方式

    using UnityEngine;   using System.Collections;   public class example : MonoBehaviour {   public voi ...

  9. (转)Spring开启Annotation<context:annotation-config> 和 <context:component-scan>诠释及区别

    转自:https://www.cnblogs.com/leiOOlei/p/3713989.html <context:annotation-config> 和 <context:c ...

  10. mybatis传递Map和List集合示例

    1.List示例 java文件: dao: public List<ServicePort> selectByIps(List<String> ips); xml文件: < ...