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. Spring Mvc Rest为不支持DELETE/PUT的浏览器添加DELETE/PUT支持

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

  2. 解压版Tomcat配置

    解压版Tomcat配置(本例Tomcat6):   一 配置Tomcat 1 下载Tomcat Zip压缩包,解压.      如果增加tomcat的用户名和密码,则修改/conf/tomcat-us ...

  3. SQL: ROW_NUMBER

  4. 培训第四天-----jdbc连接oracle

    oracle链接数据库并向tableone插入中一条数据 package com.zjw.db; import java.sql.Connection; import java.sql.DriverM ...

  5. Android中View绘制流程以及invalidate()等相关方法分析

    [原文]http://blog.csdn.net/qinjuning 整个View树的绘图流程是在ViewRoot.java类的performTraversals()函数展开的,该函数做的执行过程可简 ...

  6. 对于C(n,k)取模

    2016.1.26 法一:直接根据定义式,求乘法逆元即可 法二:借助关于n!mod p,那么根据C(n,k)的定义式并结合乘法逆元即可求解. 法三:借助卢卡斯定理求解 特别注意:在C(n,k)模p等于 ...

  7. struts的标签库出现Failed to load or instantiate TagExtraInfo class

    使用struts的标签库出现Failed to load or instantiate TagExtraInfo class 最近在使用struts标签库的时候,在eclipse开发环境中是正常的,放 ...

  8. 用iptables 实现本地端口转发

    设定本机2121端口转发到21端口 iptables -t nat -A PREROUTING -p tcp -i eth0 -d -j DNAT --to iptables -t nat -I PO ...

  9. 将声音文件加入VC

    概述 VC++是微软公司开发的一个IDE(集成开发环境),换句话说,就是使用c++的一个开发平台.有些软件就是这个编出来的...另外还有VB,VF.只是使用不同语言...但是,VC++是Windows ...

  10. zTree默认勾选指定ID并执行事件

    try { var arrs = $('#subjectClassID').val().split(","); var treeObj = $.fn.zTree.getZTreeO ...