题目链接:http://poj.org/problem?id=3311

学习博客:https://blog.csdn.net/u013480600/article/details/19692985

Hie with the Pie
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

 
题目大意:输入n,接下来有(n+1)*(n+1)的矩阵,dist[i][j]代表i和j之间的距离,刚开始在0的位置,要求你1^n,每个点至少走一次,最后回到0,所需要的最短时间是多少
思路:

分析;本题和经典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)的更多相关文章

  1. Hie with the Pie(POJ3311+floyd+状压dp+TSP问题dp解法)

    题目链接:http://poj.org/problem?id=3311 题目: 题意:n个城市,每两个城市间都存在距离,问你恰好经过所有城市一遍,最后回到起点(0)的最短距离. 思路:我们首先用flo ...

  2. poj 3311 Hie with the Pie (状压dp) (Tsp问题)

    这道题就是Tsp问题,稍微加了些改变 注意以下问题 (1)每个点可以经过多次,这里就可以用弗洛伊德初始化最短距离 (2)在循环中集合可以用S表示更清晰一些 (3)第一维为状态,第二维为在哪个点,不要写 ...

  3. 【POJ3311】Hie with the Pie(状压DP,最短路)

    题意: 思路:状压DP入门题 #include<cstdio> #include<cstdlib> #include<algorithm> #include< ...

  4. POJ3311 Hie with the Pie(状压DP,Tsp)

    本题是经典的Tsp问题的变形,Tsp问题就是要求从起点出发经过每个节点一次再回到起点的距离最小值,本题的区别就是可以经过一个节点不止一次,那么先预处理出任意两点之间的最短距离就行了,因为再多走只会浪费 ...

  5. Hie with the Pie(POJ 3311状压dp)

    题意:披萨店给n个地方送披萨,已知各地方(包括披萨店)之间花费的时间,求送完所有地方并回到店花费的最小时间 分析:状态好确定dp[i][j],i中1表示地方已送过,否则为0,j为当前状态最后一个送过的 ...

  6. POJ 3311 Hie with the Pie(DP状态压缩+最短路径)

    题目链接:http://poj.org/problem?id=3311 题目大意:一个送披萨的,每次送外卖不超过10个地方,给你这些地方之间的时间,求送完外卖回到店里的总时间最小. Sample In ...

  7. POJ 3311 Hie with the Pie (BFS+最短路+状态压缩)

    题意:类似于TSP问题,只是每个点可以走多次,求回到起点的最短距离(起点为点0). 分析:状态压缩,先预处理各点之间的最短路,然后sum[i][buff]表示在i点,状态为buff时所耗时...... ...

  8. 状压dp+floyed(C - Hie with the Pie POJ - 3311 )

    题目链接:https://cn.vjudge.net/contest/276236#problem/C 题目大意: 给你一个有n+1(1<=n<=10)个点的有向完全图,用矩阵的形式给出任 ...

  9. Hie with the Pie(状压DP+可以经过多次相同的点要全部走过的最短回路)

    大意:一个人要送n份货,给出一个矩阵,表示任意两个点间的直接路径长度,求从起点0送完这n份货(到达指定的n个地点)再回到起点0的最短时间.经过任意顶点的次数不限. 分析:既然是可以过多个点,那我们可以 ...

随机推荐

  1. ElasticSearch logo 分布式搜索引擎 ElasticSearch

    原文来自:http://www.oschina.net/p/elasticsearch Elastic Search 是一个基于Lucene构建的开源,分布式,RESTful搜索引擎.设计用于云计算中 ...

  2. 问题15:如何判断字符串a是否以字符串b开头或结尾

    方法一:使用正则表达式的^和$实现 '^000':表示,只匹配字符串的开头,若开头是 '000' ,则返回 ['000'] : '000$':表示,只匹配字符串的结尾,若结尾是 '000' ,则返回 ...

  3. js右击事件

    先贴代码: <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF ...

  4. linux日常管理-查看系统负载

    查看系统的负载常用命令w 16:32::15是系统时间 up 16 min 是开机使用时间 1 user 是登录的用户数 重要 load average:0.00 0.00 0.00 负载分别表示1分 ...

  5. 10个C语言经典

    1.计算Fibonacci数列Fibonacci数列又称斐波那契数列,又称黄金分割数列,指的是这样一个数列:1.1.2.3.5.8.13.21.C语言实现的代码如下: /* t3ing Fibonac ...

  6. Load Runner 变量、参数的简单使用

    Action(){ 定义数组时一定要指明大小 变量定义一定要放在所以操作之前,放在脚本最前面     int num ;//定义数值变量 int numy[5];//定义整型数组 char *str1 ...

  7. [dp]LCS最长公共子序列

    https://www.51nod.com/tutorial/course.html#!courseId=4 复杂度:${\rm O}(nm)$ 转移方程: #include<bits/stdc ...

  8. 3java面试题 传智 发的 有用

    第一章内容介绍 20 第二章JavaSE基础 21 一.Java面向对象 21 1. 面向对象都有哪些特性以及你对这些特性的理解 21 2. 访问权限修饰符public.private.protect ...

  9. 杭电acm 1033题

    Problem Description For products that are wrapped in small packings it is necessary that the sheet o ...

  10. From COM to COM 侯捷 1998.06.12

    摘要: 本文簡介 C++ Object Model 和 Component Object Model 的基本概念,並引介四本書籍: 1. Inside The C++ Object Model 2. ...