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的最短时间.经过任意顶点的次数不限. 分析:既然是可以过多个点,那我们可以 ...
随机推荐
- POJ1456:Supermarket
浅谈堆:https://www.cnblogs.com/AKMer/p/10284629.html 题目传送门:http://poj.org/problem?id=1456 把物品按照时间排序,显然\ ...
- Excel用vlookup方法匹配数据
(1) VLOOKUP是一个查找函数,给定一个查找的目标,它就能从指定的查找区域中查找返回想要查找到的值.它的基本语法为: VLOOKUP(查找目标,查找范围,返回值的列数,精确OR模糊查找) ...
- puppet插件fact和hiera(puppet自动化系列3)
四.Fact插件 4.1 使用pluginsync进行发布 这种方法比较特殊,节点factpath目录里除了编写好的rb文件之外,还需要在puppet模块中引用,运行一次之后才会转换成fact.通常在 ...
- 关于系统中:/dev/mem
1)参考:https://blog.csdn.net/lsn946803746/article/details/52948036 博主:lsn946803746 2)参考:https://blog ...
- 安装Ruby On Rails时运行“gem install rails”没有反应怎么办?
这两天在我的mac机上安装Ruby On Rails,感觉很爽,似乎在使用一个Windows和Linux的结合体,要界面有界面,要命令行有命令行. 不过安装Ruby On Rails的过程中遇到一个问 ...
- Mac系统的launchd、守护进程daemon(2013笔记整理)
1. launchd Mac系统下通用的进程管理器,是Mac系统下非常重要的一个进程,一般来说该进程不允许直接以命令行的形式调用.只能通过其控制管理界面,launchctl来进行控制. launchd ...
- Loadrunner 监控 Linux (centos6.5)服务器系统资源
Loadrunner 监控 Linux 服务器系统资源,需要在被监控的服务器上启用 rstatd 进程但尝试启动时,爆炸了: [root@test1 rpc.rstatd-4.0.1]# rpc.rs ...
- Maven的pom实例
该pom中包含了一些我认为会需要的东西,并且加了注释.可以根据需求适当删减. 包含了spring-mvc , junit,hibernate验证,json,apache-commons组件 还有 co ...
- 22、IDP-ASE
IDPASE https://github.com/bdeonovic/IDPASE.jl Prepare necessary input files (1)FASTQ file of your hy ...
- 阶段2-新手上路\项目-移动物体监控系统\Sprint4-嵌入式web服务器开发\第3课-CGI程序开发
实现CGI程序显示一幅图片最核心的功能 把上一节课编写好的led.c程序拷贝过来,并重新命名为image.c 把led的某些部分删除,后如下 那么如何显示一幅图片呢,百度(搜索在html里面去插入图片 ...