POJ3311 Hie with the Pie
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
/*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
const int mxn=<<;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int f[mxn][];
int mp[][];
bool inq[mxn];
int main(){
int i,j;
int n;
while(scanf("%d",&n) && n){
for(i=;i<=n;i++)
for(j=;j<=n;j++)
mp[i][j]=read();
memset(f,0x3f,sizeof f);
int ed=(<<(n+))-;
f[][]=;
queue<int>q;
q.push();
while(!q.empty()){
int u=q.front();q.pop();inq[u]=;
for(i=;i<=n;i++){
if(i==u)continue;
for(int k=;k<=ed;k++){
if(!(k&(<<u)))continue;
if(f[k|(<<i)][i]>f[k][u]+mp[u][i]){
f[k|(<<i)][i]=f[k][u]+mp[u][i];
if(!inq[i]){
inq[i]=;
q.push(i);
}
}
}
}
}
printf("%d\n",f[ed][]);
}
return ;
}
POJ3311 Hie with the Pie的更多相关文章
- POJ3311 Hie with the Pie 【状压dp/TSP问题】
		
题目链接:http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total ...
 - poj3311 Hie with the Pie (状态压缩dp,旅行商)
		
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 3160 Accepted: 1613 ...
 - [TSP+floyd]POJ3311 Hie with the Pie
		
题意: 给i到j花费的地图 1到n编号 一个人要从1遍历n个城市后回到1 求最小的花费(可以重复走) 分析 http://www.cnblogs.com/Empress/p/4039240.htm ...
 - [poj3311]Hie with the Pie(Floyd+状态压缩DP)
		
题意:tsp问题,经过图中所有的点并回到原点的最短距离. 解题关键:floyd+状态压缩dp,注意floyd时k必须在最外层 转移方程:$dp[S][i] = \min (dp[S \wedge (1 ...
 - 【鸽】poj3311 Hie with the Pie[状压DP+Floyd]
		
题解网上一搜一大坨的,不用复述了吧. 只是觉得网上dp方程没多大问题,但是状态的表示含义模糊.不同于正常哈密顿路径求解,状态表示应当改一下. 首先定义一次移动为从一个点经过若干个点到达另一个点,则$f ...
 - POJ3311 Hie with the Pie(状压DP,Tsp)
		
本题是经典的Tsp问题的变形,Tsp问题就是要求从起点出发经过每个节点一次再回到起点的距离最小值,本题的区别就是可以经过一个节点不止一次,那么先预处理出任意两点之间的最短距离就行了,因为再多走只会浪费 ...
 - Hie with the Pie(poj3311)
		
题目链接:http://poj.org/problem?id=3311 学习博客:https://blog.csdn.net/u013480600/article/details/19692985 H ...
 - Hie with the Pie
		
Hie with the Pie poj-3311 题目大意:n+1个点,伪旅行商问题. 注释:n<=10. 想法:咳咳,第一道状压dp,下面我来介绍一下状压dp. 所谓dp,就是动态性决策规划 ...
 - poj 3311    Hie with the Pie
		
floyd,旅游问题每个点都要到,可重复,最后回来,dp http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS Me ...
 
随机推荐
- JavaScript中的match方法和search方法
			
search在一个字串对象(string object)中查找关键词字串(规范表达式,regular expression),若匹配(即在目标字串中成功找到关键词)则返回关键词在目标字串中第一次出现的 ...
 - ELFhash - 优秀的字符串哈希算法
			
ELFhash - 优秀的字符串哈希算法 2016年10月29日 22:12:37 阅读数:6440更多 个人分类: 算法杂论算法精讲数据结构 所属专栏: 算法与数据结构 版权声明:本文为博主原创 ...
 - 抽屉head部分,hover应用,鼠标放上变色
			
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
 - Centos7之Nginx
			
1.安装 下载RPM: wget http://nginx.org/download/nginx-1.16.0.tar.gz 解压:tar -zxf nginx-1.16.0.tar.gz 安装: c ...
 - composer 类加载器,对 <PSR-4的风格>、<PSR-0的风格>、<PEAR的风格> 风格的类的加载
			
class ClassLoader { // ... /** * composer 类加载器,对 <PSR-4的风格>.<PSR-0的风格>.<PEAR的风格> 风 ...
 - 传智 杨中科老师 ASP.NET 笔记
 - 动态规划:HDU1864-最大报销额(处理带小数的dp问题)
			
最大报销额 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Descriptio ...
 - HTTP认证之摘要认证——Digest(一)
			
导航 HTTP认证之基本认证--Basic(一) HTTP认证之基本认证--Basic(二) HTTP认证之摘要认证--Digest(一) HTTP认证之摘要认证--Digest(二) 一.概述 Di ...
 - Sql日期时间格式转换(转 子夜.)
			
sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007-0 ...
 - JQuery easyUi datagrid 中 自定义editor作为列表操作按钮列
			
转自 http://blog.csdn.net/tianlincao/article/details/7494467 前言 JQuery easyUi datagrid 中 使用datagrid生 ...