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推 ...
随机推荐
- 欧拉工程第70题:Totient permutation
题目链接 和上面几题差不多的 Euler's Totient function, φ(n) [sometimes called the phi function]:小于等于n的数并且和n是互质的数的个 ...
- tomcat源码导入eclipse
1. 获取源代码 方式一:从官网http://tomcat.apache.org/download-70.cgi 直接下载,官网提供了Binary 和 Source Code两种下载方式,要研究tom ...
- 【最新】最流行的java后台框架 springmvc mybaits 集代码生成器 SSM SSH
获取[下载地址] QQ: 313596790 [免费支持更新] A 代码生成器(开发利器);全部是源码 增删改查的处理类,service层,mybatis的xml,SQL( m ...
- 坑爹的Mysql
本想尝试下如何使用Spring来管理Hibernate的事务,当配置好Spring的配置文件后,进行插入数据,结果报错了,错误是: Mysql Field * doesn't have a defau ...
- 非常实用的PHP代码片段推荐
当使用PHP进行开发的时候,如果你自己收 藏 了一些非常有用的方法或者代码片段,那么将会给你的开发工作带来极大的便利.今天我们将介绍10个超级好用的PHP代码片段,希望大家能够喜欢! 1. 使用te ...
- BZOJ 1898 Swamp 沼泽鳄鱼(矩阵)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1898 题意:一个无向图.给出起点和终点,以及某些时刻某些点不能到达的信息.问从起点出发在 ...
- n人比赛,可轮空,比赛轮数和场数
#include<stdio.h> int chang(int x,int s){ ) return s; ) ; !=){ s+=(x-)/; )/,s); } else{ s+=x/; ...
- JAVA将Excel中的报表导出为图片格式(三)换一种实现
上一篇介绍了使用Java的Robot机器人实现截图,然后将剪贴板上的数据流生成PNG图片 但是经过博主的不断测试,在完全依赖远程桌面的没有终端显示器的服务器上 使用截图方式是不可行的,因为一旦使用了远 ...
- BootStrap图标
- uva12169 Disgruntled Judge
扩展欧几里得. 枚举a,根据x1,x3和递推式可得. (a+1)*b-k*mod=f[3]-a*a*b. 通过扩展欧几里得求出b. 带入原式进行计算. #include<cstdio> # ...