poj3311 Hie with the Pie (状态压缩dp,旅行商)
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
#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,旅行商)的更多相关文章
- 【鸽】poj3311 Hie with the Pie[状压DP+Floyd]
题解网上一搜一大坨的,不用复述了吧. 只是觉得网上dp方程没多大问题,但是状态的表示含义模糊.不同于正常哈密顿路径求解,状态表示应当改一下. 首先定义一次移动为从一个点经过若干个点到达另一个点,则$f ...
- POJ 3311 Hie with the Pie:TSP(旅行商)【节点可多次经过】
题目链接:http://poj.org/problem?id=3311 题意: 你在0号点(pizza店),要往1到n号节点送pizza. 每个节点可以重复经过. 给你一个(n+1)*(n+1)的邻接 ...
- [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 我还加了一些注释代码,对 ...
- poj 3311 Hie with the Pie(状态压缩dp)
Description The Pizazz Pizzeria prides itself or more (up to ) orders to be processed before he star ...
- 状态压缩DP(大佬写的很好,转来看)
奉上大佬博客 https://blog.csdn.net/accry/article/details/6607703 动态规划本来就很抽象,状态的设定和状态的转移都不好把握,而状态压缩的动态规划解决的 ...
- poj 3311 floyd+dfs或状态压缩dp 两种方法
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6436 Accepted: 3470 ...
- DP大作战—状态压缩dp
题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...
- hoj2662 状态压缩dp
Pieces Assignment My Tags (Edit) Source : zhouguyue Time limit : 1 sec Memory limit : 64 M S ...
随机推荐
- JAVA源码走读(一) HashMap与ArrayList
HashMap 一.HashMap基本概念: HashMap是基于哈希表的Map接口的实现.此实现提供所有可选的映射操作,并允许使用null值和null键.此类不保证映射的顺序,特别是它不保证该顺序恒 ...
- Java数组在内存中是如何存放的
阅读目录 一维数组 二维数组 数组对象及其引用存放在内存中的哪里? Java中有两种类型的数组: 基本数据类型数组: 对象数组: 当一个对象使用关键字“new”创建时,会在堆上分配内存空间,然后返回对 ...
- Programming in Lua读书笔记
Lua的长处之一就是可以通过新类型和函数来扩展其功能.动态类型检查最大限度允许多态出现,并自动简化调用内存管理的接口,因为这样不需要关心谁来分配内存谁来释放内存,也不必担心数据溢出.高级函数 ...
- 【摘】crontab 各时间含义
HELL=/bin/bash <==使用哪種 shell 介面 PATH=/sbin:/bin:/usr/sbin:/usr/bin <==執行檔搜尋路徑 MAILTO=root < ...
- Esfog_UnityShader教程_NormalMap法线贴图
咳咳,好久没有更新了,一来是这段时间很忙很忙,再来就是自己有些懒了,这个要不得啊,赶紧补上.在前面我们已经介绍过了漫反射和镜面反射,这两个是基本的光照类型,仅仅依靠它们就想制作出精美的效果是远远不够的 ...
- HBase自动分区
HBase扩展和负载均衡的基本单位是Region.Region从本质上说是行的集合.当Region的大小达到一定的阈值,该Region会自动分裂(split),当然也可能是合并(merge),合并可以 ...
- .net 生成 静态页面
.net 生成 静态页面 <!--Main.Aspx--> <%@ page language="C#" %> <%@ import namespac ...
- IntelliJ IDEA - 注释模板
IntelliJ IDEA 注释模板自定义的方式有许多,如Live Templates和File and Code Templates,我比较喜欢File and Code Templates,在新建 ...
- MSSQL PIVOT 实现行列转置
create table #temp ( ProdStep varchar(40), ModuleStatus varchar(40), Cnt int ); insert into #temp va ...
- Struts2从一个action转到另一个action的两种方法
在Struts2中,Action处理完用户请求后,将会返回一个字符串对象,这个字符串对象就是一个逻辑视图名.Struts 2通过配置逻辑视图名和物理视图之间的映射关系,一旦系统收到Action返回的某 ...