Hie with the Pie

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 3160   Accepted: 1613

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

Source

 
用二进制位表示某一点是否走过,1表示走过,0表示没走过,例如如果有四个点,3的二进制是(0011)2,就表示1点和2点走过,3点和4点没走,用dp(i,j)表示状态为i时,走到j点的最短距离,状态i就是前面所说的二进制标记,那么在i点j为1,2时才有效(通过 i & (1<<(j - 1))来检测在状态i下哪一位是1),根据此状态,能更新dp(7,3)和dp(11,4),即更新二进制位下为0的点,7是(0111)2,11是(1011)2
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int N = ;
const int INF = 0x7f7f7f7f;
int graph[N][N];
int dp[(<<)+][];
void floyd(int n) {
int i,j,k;
for (k = ; k <= n; k++) {
for (i = ; i <= n; i++) {
for (j = ; j <= n; j++) {
if (graph[i][k] + graph[k][j] < graph[i][j])
graph[i][j] = graph[i][k] + graph[k][j];
}
}
}
}
int main() {
// freopen("in.txt","r",stdin);
int n;
int i,j,k;
while (scanf("%d",&n) && n) {
for (i = ; i <= n; i++) {
for (j = ; j <= n; j++)
scanf("%d",&graph[i][j]);
}
floyd(n);
memset(dp, 0x7f, sizeof(dp));
for (i = ; i <= n; i++) {
dp[<<(i - )][i] = graph[][i];
}
for (i = ; i < (<<n); i++) {
for (j = ; j <= n; j++) {
if ((i & ( << (j - ))) == ) {
for (k = ; k <= n; k++) {
if (i & ( << (k - ))) {
dp[(i|( << (j - )))][j] = min(dp[(i|( << (j - )))][j], dp[i][k] + graph[k][j]);
}
}
}
}
}
int mini = INF;
for(i = ; i <= n; i++) {
if (dp[(<<n) - ][i] + graph[i][] < mini)
mini = dp[(<<n) - ][i] + graph[i][];
}
printf("%d\n",mini);
}
return ;
}

poj3311 Hie with the Pie (状态压缩dp,旅行商)的更多相关文章

  1. 【鸽】poj3311 Hie with the Pie[状压DP+Floyd]

    题解网上一搜一大坨的,不用复述了吧. 只是觉得网上dp方程没多大问题,但是状态的表示含义模糊.不同于正常哈密顿路径求解,状态表示应当改一下. 首先定义一次移动为从一个点经过若干个点到达另一个点,则$f ...

  2. POJ 3311 Hie with the Pie:TSP(旅行商)【节点可多次经过】

    题目链接:http://poj.org/problem?id=3311 题意: 你在0号点(pizza店),要往1到n号节点送pizza. 每个节点可以重复经过. 给你一个(n+1)*(n+1)的邻接 ...

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

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

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

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

  5. poj 3311 Hie with the Pie(状态压缩dp)

    Description The Pizazz Pizzeria prides itself or more (up to ) orders to be processed before he star ...

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

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

  7. poj 3311 floyd+dfs或状态压缩dp 两种方法

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6436   Accepted: 3470 ...

  8. DP大作战—状态压缩dp

    题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...

  9. hoj2662 状态压缩dp

    Pieces Assignment My Tags   (Edit)   Source : zhouguyue   Time limit : 1 sec   Memory limit : 64 M S ...

随机推荐

  1. Java多线程系列- DelayQueue延时队列

    我们在开发中,有如下场景 a) 关闭空闲连接.服务器中,有很多客户端的连接,空闲一段时间之后需要关闭之.b) 缓存.缓存中的对象,超过了空闲时间,需要从缓存中移出.c) 任务超时处理.在网络协议滑动窗 ...

  2. coderforces 721b

    题目描述: B. Passwords time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. Spring Mvc Rest为不支持DELETE/PUT的浏览器添加DELETE/PUT支持

    现在都流行RESTFul,但是有一个问题,有些浏览器现在就不支持delete/put方式的请求,这些请求发出去之后都会变成get请求,导致rest接口无法被访问到.为了解决这个问题,spring提出了 ...

  4. Spring Integration

    @ContextConfiguration directs Spring's test runner to locate a configuration file with the same name ...

  5. dubbo-admin和dubbo-monitor-simple的布署

    dubbo-admin 把dubbo-admin的war包解压到tomcat或者jetty的ROOT目录下,启动就可以访问了(自己配置tomcat的端口) wget http://code.aliba ...

  6. 前端网站helper

    聚合api 一.颜色类网站http://colorhunt.co 这个网站给我们提供了很多的配色方案,我们直接使用就OK了.使用方法也很简单,鼠标移动到对应的颜色上,我们就可以看到颜色的十六进制码,复 ...

  7. [转载]《民航科技》2012年4月专家论坛:罗喜伶《SWIM技术国际研究动态及对中国民航的借鉴意义》

    专家介绍:罗喜伶,北京航空航天大学电子信息工程学院副教授,工学博士,硕士生导师,国家空管新航行系统技术重点实验室和协同式网络化空中交通管理系统研究教育部创新团队核心成员,民航空管广域信息系统专家组成员 ...

  8. web页面设计基本原则

    1.对齐. 不对齐看起来糟糕极了.简直一片混乱,会给人一种很不专业的印象.对齐让页面更漂亮一些,更重要的是便于更好的传达信息. 2.去掉糟糕的边框.边框会产生一种分离感. 3.亲密性. 元素相互靠近看 ...

  9. SpringMVC环境搭建 配置文件_2

    applicationContext 命名空间: 引入命名空间,这样可以在代码中使用annotation xmlns="http://www.springframework.org/sche ...

  10. 一段比较有意思的代码——介绍system verilog中的新增幅值语句

    system verilog中新加了很多幅值语句,虽然都只适用于阻塞幅值,但是在某些场合中非常实用. 下面是一段有意思的代码,覆盖了一些用法. package definitions; typedef ...