标签:记忆化搜索 n+1 一点 return clu Once numbers test art
题目链接: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=10+5; const int maxm=1<<maxn; const int maxx=1e4+10; const ll maxe=1000+10; #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[15][maxm]; void floyed() { for(int i=0;i<=n;i++) { for(int j=0;j<=n;j++) { for(int k=0;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=0;j<=n;j++)//集合s中的一位j { if(s&(1<<j)&&j!=i)//j==i代表在同一点 所以要!= { ans=min(ans,solve(j,s^(1<<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==0) break; for(int i=0;i<=n;i++) { for(int j=0;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[0][0]=0;//dp[0][0] 在0点 集合为空 vis[0][0]=true;//已经知道距离的为true for(int i=1;i<=n;i++) { vis[i][1<<i]=true;//已经知道距离的为true dp[i][1<<i]=min_dist[0][i];//在i点,集合里只有本身,距离也就是最短路求出来的两点间最短距离 } cout<<solve(0,(1<<(n+1))-1)<<endl;//注意这里为何是(1<<(n+1))-1呢,因为有n+1位(加上0那一位),你需要的状态是n+1位都为1,(1<<(n+1))-1就可以得到了 } return 0; }
标签:记忆化搜索 n+1 一点 return clu Once numbers test art
原文地址:https://www.cnblogs.com/caijiaming/p/9744633.html