Hie with the Pie(poj3311)
题目链接:http://poj.org/problem?id=3311
学习博客:https://blog.csdn.net/u013480600/article/details/19692985
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 9954 | Accepted: 5368 |
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
分析;本题和经典TSP旅行商问题类似,但是TSP要求每个节点仅走过1次,本题要求每个节点至少走过一次。
同样的是本题也用状态压缩DP来解。令d[i][s]表示从0号点出发,走过集合S中的所有点(不包含起始0号点,但可以包含终止点0号点),且当前在i号点时所需的最小距离。
则:d[i][s]= min{ d[ j ][ s –{i}]+min_dist[j][i] } 其中min_dist[j][i]是指从j节点到i节点的最小距离, s表示节点集合,且包含i和j。
初值:d[ 0 ][ {} ] = 0,我们所求最小距离为:d[0][{0,1,2,3,…n}]
接下来我们来论证一下该题的正确性:首先 :d[0][{0,1,2,3,…n}]表示最终走过所有节点至少1次回到0号点的最小距离。则当走最后一步从i到0时,则必然d[0][{0,1,2,3,…n}] 是从所有的 d[i][{1,2,3,…n}] + min_dist[i][0]中选一个最小的值,1<=i<=n。
d[i][{1,2,3,…n}]也必然是从所有的d[j][{1,2,3…n}-{i}] + min_dist[j][i]中选一个最小的值,以此类推。所以行走最短路线的过程只能是我们上面的分析过程,不可能有其他方式,如果存在最短路径我们必然可以通过以上解法求出来。
由于本题要求的是min_dist[i][j]即两点间的最短距离,且不在乎重复走过一些点,所以需要先用floyd算法先求出任意两点间的最小距离。
有个疑问,假设d[2][{1,2}]= 10,且dist[2][3]=10(2到3的真实距离为10)假设min_dist[2][3]=5,因为2到1再到3的距离为5,所以我们会求得d[3][{1,2,3}]=15吗?此时我们走了两次1号节点,有没有可能d[3][{1,2,3}]的值更小一点,而我们却漏了这个解?不可能的,我们当前走的路线是0->1->2->1->3得到的15,如果0->2->1->3得到的值小于15那么d[3][{1,2,3}]的更小值肯定能从d[1][{1,2}]得到更新。所以这个递推方程是不会丢失最优解的。
看代码:
#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9;
const int maxn=+;
const int maxm=<<maxn;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int n;
int dist[maxn][maxn];//输入的距离矩阵
int min_dist[maxn][maxn];//求出的最短路径距离矩阵
int dp[maxn][maxm];//用于表示dp状态,dp[i][{}]表示此时在i点,走过的点构成的集合{},集合用二进制存储
bool vis[][maxm];
void floyed()
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
for(int k=;k<=n;k++)
{
min_dist[j][k]=min(min_dist[j][k],min_dist[j][i]+min_dist[i][k]);
}
}
}
}
int solve(int i,int s)//记忆化搜索,这个函数不会计算solve(0,0)
{
if(vis[i][s]) return dp[i][s];//已经知道最短距离了
vis[i][s]=true;//下面是寻找的过程,跟dfs有点相像
int &ans=dp[i][s];//这里用了一个引用,改变ans的同时也改变了dp[i][s]
ans=mod;//赋值为无穷大
for(int j=;j<=n;j++)//集合s中的一位j
{
if(s&(<<j)&&j!=i)//j==i代表在同一点 所以要!=
{
ans=min(ans,solve(j,s^(<<i))+min_dist[j][i]);//为什么是solve(j,s^(1<<i)) ,因为j变为起点了,同时要去掉i这一位,^运算相同为0,相异为1
}
}
return ans;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==) break;
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
cin>>dist[i][j];
min_dist[i][j]=dist[i][j];
}
}
floyed();//求出两点的最短距离
//for(int i=1;i<=3;i++) cout<<min_dist[0][i]<<" ";
//cout<<endl;
/*
for(int i=0;i<=3;i++)
{
for(int j=0;j<=3;j++)
cout<<min_dist[i][j]<<" ";//不理解的话可以用这个输出来理解一下,因为我们已经求出了任意两点之间的最短距离了,所以这样是不会漏掉最优解的
cout<<endl;
}
*/
memset(vis,false,sizeof(vis));
dp[][]=;//dp[0][0] 在0点 集合为空
vis[][]=true;//已经知道距离的为true
for(int i=;i<=n;i++)
{
vis[i][<<i]=true;//已经知道距离的为true
dp[i][<<i]=min_dist[][i];//在i点,集合里只有本身,距离也就是最短路求出来的两点间最短距离
}
cout<<solve(,(<<(n+))-)<<endl;//注意这里为何是(1<<(n+1))-1呢,因为有n+1位(加上0那一位),你需要的状态是n+1位都为1,(1<<(n+1))-1就可以得到了
}
return ;
}
Hie with the Pie(poj3311)的更多相关文章
- Hie with the Pie(POJ3311+floyd+状压dp+TSP问题dp解法)
题目链接:http://poj.org/problem?id=3311 题目: 题意:n个城市,每两个城市间都存在距离,问你恰好经过所有城市一遍,最后回到起点(0)的最短距离. 思路:我们首先用flo ...
- poj 3311 Hie with the Pie (状压dp) (Tsp问题)
这道题就是Tsp问题,稍微加了些改变 注意以下问题 (1)每个点可以经过多次,这里就可以用弗洛伊德初始化最短距离 (2)在循环中集合可以用S表示更清晰一些 (3)第一维为状态,第二维为在哪个点,不要写 ...
- 【POJ3311】Hie with the Pie(状压DP,最短路)
题意: 思路:状压DP入门题 #include<cstdio> #include<cstdlib> #include<algorithm> #include< ...
- POJ3311 Hie with the Pie(状压DP,Tsp)
本题是经典的Tsp问题的变形,Tsp问题就是要求从起点出发经过每个节点一次再回到起点的距离最小值,本题的区别就是可以经过一个节点不止一次,那么先预处理出任意两点之间的最短距离就行了,因为再多走只会浪费 ...
- Hie with the Pie(POJ 3311状压dp)
题意:披萨店给n个地方送披萨,已知各地方(包括披萨店)之间花费的时间,求送完所有地方并回到店花费的最小时间 分析:状态好确定dp[i][j],i中1表示地方已送过,否则为0,j为当前状态最后一个送过的 ...
- POJ 3311 Hie with the Pie(DP状态压缩+最短路径)
题目链接:http://poj.org/problem?id=3311 题目大意:一个送披萨的,每次送外卖不超过10个地方,给你这些地方之间的时间,求送完外卖回到店里的总时间最小. Sample In ...
- POJ 3311 Hie with the Pie (BFS+最短路+状态压缩)
题意:类似于TSP问题,只是每个点可以走多次,求回到起点的最短距离(起点为点0). 分析:状态压缩,先预处理各点之间的最短路,然后sum[i][buff]表示在i点,状态为buff时所耗时...... ...
- 状压dp+floyed(C - Hie with the Pie POJ - 3311 )
题目链接:https://cn.vjudge.net/contest/276236#problem/C 题目大意: 给你一个有n+1(1<=n<=10)个点的有向完全图,用矩阵的形式给出任 ...
- Hie with the Pie(状压DP+可以经过多次相同的点要全部走过的最短回路)
大意:一个人要送n份货,给出一个矩阵,表示任意两个点间的直接路径长度,求从起点0送完这n份货(到达指定的n个地点)再回到起点0的最短时间.经过任意顶点的次数不限. 分析:既然是可以过多个点,那我们可以 ...
随机推荐
- WPF中Xaml编译正常而Designer Time时出错的解决办法
开发wpf时我们经常遇到一个xaml文件在设计时显示解析错误(比如在:VS或者Blend)而编译正常运行正常. 原因是:xaml的在Debug版本下必须为anyCPU. 解决办法: 1.打开工程文件x ...
- 杂项-Log:NLog
ylbtech-杂项-Log:NLog NLog是一个基于.NET平台编写的类库,我们可以使用NLog在应用程序中添加极为完善的跟踪调试代码. NLog是一个简单灵活的.NET日志记录类库.通过使用N ...
- VisualGDB系列7:使用VS创建Linux静态库和动态库
根据VisualGDB官网(https://visualgdb.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指正. 本文介绍如何在VS中创建静态库和动态库, ...
- [jQuery] 按回车键实现登录
Jquery按回车键提交实现登录的方式分为两种: 1.按钮提交 2.表单提交 1.按钮提交 $("#LoginIn").off('click').on('click', funct ...
- sql server 表索引碎片处理
DBCC SHOWCONTIG (Transact-SQL) SQL Server 2005 其他版本 更新日期: 2007 年 9 月 15 日 显示指定的表或视图的数据和索引的碎片信息. 重要提示 ...
- LAMP 1.8默认虚拟主机
默认虚拟主机是为了解决别人域名恶心绑定自己的服务器ip,可导致服务器上的网站排名靠后,即干扰seo优化 我们访问指定的两个网站可以直接访问,ip也可以访问 打开配置文件 vim /usr/local/ ...
- [hdu3549]Flow Problem(最大流模板题)
解题关键:使用的挑战程序设计竞赛上的模板,第一道网络流题目,效率比较低,且用不习惯的vector来建图. 看到网上其他人说此题有重边,需要注意下,此问题只在邻接矩阵建图时会出问题,邻接表不会存在的,也 ...
- shell script-判断式
test判断式 看下面: test -e /opt/a.txt && echo "exist" || echo "not exist" 判断 / ...
- Static与Const的区别
static static局部变量 将一个变量声明为函数的局部变量,那么这个局部变量在函数执行完成之后不会被释放,而是继续保留在内存中 static 全局变量 表示一个变量在当前文件的全局内可访问 s ...
- 使用python内置模块os和openpyxl搜索指定文件夹下Excel中的内容
在指定路径下,搜索Excel文件中包含的指定内容,首先需要遍历指定路径,得到该路径下所有Excel文件的绝对/相对路径:然后读取Excel中内容,将文件中的每个单元格的值与要搜索的内容进行判断(正则比 ...