poj 3311 Hie with the Pie
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 4013 | Accepted: 2132 |
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 jwithout 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点出发,送外卖到n个地方,给出每个点之间的距离,帮他选择一条最短路回到原点。注意他走的节点可以重复。
分析:
先用floyd求每俩个点,然后进行dp,dp[i][j],表示最后到达i城市j状态,1表示以到达城市,0未到达。
是用状态来更新城市,比如011,最后到达第1个城市可以从010改变而来,则010就必须要有值。
具体见代码解释。先不考虑回来,只算到状态全1的情况,看哪个城市最后到达。
//dp[i][j],最后到达城市i,状态j,最小的路径。
#include<iostream>
#include<cstring>
#include<cstdio>
#define inf 1<<27
using namespace std;
int dis[][],dp[][];
int n;
void floyd()//求俩点的最短距离。
{
int i,j,k;
for(k=; k<=n; k++)
for(i=; i<=n; i++)
for(j=; j<=n; j++)
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
int main()
{
int i,j,s,k;
while(~scanf("%d",&n)&&n!=)
{
for(i=; i<=n; i++)
for(j=; j<=n; j++)
scanf("%d",&dis[i][j]);
floyd();
for(s=; s<<<n; s++)//枚举所有的状态。
{
for(i=;i<=n; i++)//除去0城市。
{
if(s&<<(i-))//是否到达i个城市,s状态的i位是否为1.
{
if(s==<<(i-))//状态只能从0到达i个城市。
dp[i][s]=dis[][i];//初始化。dp的边界。
else//状态除了0外还可以从别的城市出发,但这个城市一定是状态已经为1的城市,到达i城市。
{
dp[i][s]=inf;
for(k=; k<=n; k++)
if((k!=i)&&(s&<<(k-)))//k一定是s状态里已经到达的为1.
dp[i][s]=min(dp[i][s],dp[k][s^(<<(i-))]+dis[k][i]);
//s^(1<<(i-1)),s的i位一定是1,但要变为0,处理。 }
}
}
}
int ans=inf;
for(i=;i<=n;i++)
{ ans=min(ans,dp[i][(<<n)-]+dis[i][]);
//先不考虑回来,只算到状态全1的情况,看哪个城市最后到达。 }
printf("%d\n",ans);
}
return ;
}
poj 3311 Hie with the Pie的更多相关文章
- poj 3311 Hie with the Pie dp+状压
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4671 Accepted: 2471 ...
- poj 3311 Hie with the Pie (TSP问题)
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4491 Accepted: 2376 ...
- POJ 3311 Hie with the Pie 最短路+状压DP
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11243 Accepted: 5963 ...
- POJ 3311 Hie with the Pie(DP状态压缩+最短路径)
题目链接:http://poj.org/problem?id=3311 题目大意:一个送披萨的,每次送外卖不超过10个地方,给你这些地方之间的时间,求送完外卖回到店里的总时间最小. Sample In ...
- POJ 3311 Hie with the Pie(状压DP + Floyd)
题目链接:http://poj.org/problem?id=3311 Description The Pizazz Pizzeria prides itself in delivering pizz ...
- [POJ 3311]Hie with the Pie——谈论TSP难题DP解决方法
主题连接: id=3311">http://poj.org/problem?id=3311 题目大意:有n+1个点,给出点0~n的每两个点之间的距离,求这个图上TSP问题的最小解 ...
- POJ 3311 Hie with the Pie floyd+状压DP
链接:http://poj.org/problem?id=3311 题意:有N个地点和一个出发点(N<=10),给出全部地点两两之间的距离,问从出发点出发,走遍全部地点再回到出发点的最短距离是多 ...
- POJ 3311 Hie with the Pie:TSP(旅行商)【节点可多次经过】
题目链接:http://poj.org/problem?id=3311 题意: 你在0号点(pizza店),要往1到n号节点送pizza. 每个节点可以重复经过. 给你一个(n+1)*(n+1)的邻接 ...
- POJ 3311 Hie with the Pie (状压DP)
dp[i][j][k] i代表此层用的状态序号 j上一层用的状态序号 k是层数&1(滚动数组) 标准流程 先预处理出所有合法数据存在status里 然后独立处理第一层 然后根据前一层的max推 ...
随机推荐
- Xamarin for Visual Studio 3.11.658 Alpha 版 破解补丁
注意:此版本为 Alpha 版,版本迭代较频繁,仅供尝鲜 前提概要 全新安装请参考 安装 Xamarin for Visual Studio. 最新稳定版请参考 Xamarin for Visual ...
- Android SeekBar的OnSeekBarChangeListener
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { /** * 拖动中数值的时候 * @param f ...
- lintcode 中等题:majority number III主元素III
题目 主元素 III 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的1/k. 样例 ,返回 3 注意 数组中只有唯一的主元素 挑战 要求时间复杂度为O(n),空间复杂度为O( ...
- 纯后台生成highcharts图片有哪些方法?
比如说,领导抛给你一个需求,把一些数据做成图表,每天通过邮件发送,让领导能在邮件中就看到图片,你会有什么思路呢?本人使用的是phantomjs这个神器,它的内核是WebKit引擎,不提供图形界面,只能 ...
- 撤销 git reset --hard HEAD~1
方法一: 1.先通过git reflog找到上一次的历史提交记录id,git如果没有特意设置,是会保存记录一段时间的(a few days or a month) 2.git reset --hard ...
- HDU 4864 (2014 Multi-University Training Contest 1 )
考试时,想到了一个很类似的方法,但是总是差那么点,就是这么点,需要不断的努力啊!!! 题解: 基本思想是贪心. 对于价值c=500*xi+2*yi,yi最大影响100*2<500,所以就是求xi ...
- UVA 11294 Wedding(2-sat)
2-sat.不错的一道题,学到了不少. 需要注意这么几点: 1.题目中描述的是有n对夫妇,其中(n-1)对是来为余下的一对办婚礼的,所以新娘只有一位. 2.2-sat问题是根据必然性建边,比如说A与B ...
- BZOJ 1610 连线游戏
BZOJ不允许除以0. #include<iostream> #include<cstdio> #include<cstring> #include<cstd ...
- windows ODBC数据源里没有Oracle的驱动程序
windows ODBC数据源里没有Oracle的驱动程序 直接在“控制面板---管理工具----数据源(ODBC)” 打开数据源配置,发现只有SQLServer的驱动,其他的都没有了. ...
- LeetCode Factorial Trailing Zeroes (阶乘后缀零)
题意:如标题 思路:其他文章已经写过,参考其他. class Solution { public: int trailingZeroes(int n) { <? n/: n/+trailingZ ...