原题:
John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane and starts visiting beautiful places. To save money, John must determine the shortest closed tour that connects his destinations. Each destination is represented by a point in the plane p i =< x i ,y i >. John uses the following strategy: he starts from the leftmost point, then he goes strictly left to right to the rightmost point, and then he goes strictly right back to the starting point. It is known that the points have distinct x-coordinates. Write a program that, given a set of n points in the plane, computes the shortest closed tour that connects the points according to John’s strategy.
Input
The program input is from a text file. Each data set in the file stands for a particular set of points. For
each set of points the data set contains the number of points, and the point coordinates in ascending
order of the x coordinate. White spaces can occur freely in input. The input data are correct.
Output
For each set of data, your program should print the result to the standard output from the beginning
of a line. The tour length, a floating-point number with two fractional digits, represents the result.
Note: An input/output sample is in the table below. Here there are two data sets. The first one
contains 3 points specified by their x and y coordinates. The second point, for example, has the x
coordinate 2, and the y coordinate 3. The result for each data set is the tour length, (6.47 for the first
data set in the given example).
Sample Input
3
1 1
2 3
3 1
4
1 1
2 3
3 1
4 2
Sample Output
6.47
7.89
中文:
给你二维平面上一堆点,按照x递增的顺序给出,让你从第一个点走到最后一个点,然后再从最后一个点走回第一个点。除了起点和终点,每个点只能走一次,问你往返路程和最短是多少?
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1001;
double x[maxn],y[maxn],dp[maxn][maxn];
double dist(int i,int j)
{
return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
}
int main()
{
ios::sync_with_stdio(false);
int n;
while(cin>>n)
{
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
cin>>x[i]>>y[i];
for(int i=n-1;i>=2;i--)
{
for(int j=1;j<i;j++)
{
if(i==n-1)
dp[i][j]=dist(i,n)+dist(j,n);
else
dp[i][j]=min(dist(i,i+1)+dp[i+1][j],dist(j,i+1)+dp[i+1][i]);
}
}
cout<<fixed<<setprecision(2)<<dist(1,2)+dp[2][1]<<endl;
}
return 0;
}
思路:
此题目是紫书上的例题,也是非常经典的一道动态规划题目,叫做双调欧几里得旅行商问题,可在《算法导论》上面见到。
紫书上面给出的解决方案非常巧妙,首先把往返的问题变成两个人分别同时从起点向终点出发,并且不走同一个点。
设置状态 dp[i][j] d p [ i ] [ j ] 表示第一个人走到 i i 第二个人走到时,还需要走的距离最短是多少,此外,为了能够实现状态转移,限制 i>j i > j 且下一步的方案只能行走到 i+1 i + 1 。
决策方案只有两种,一种是在i处的人走到i+1,另一种是在j处的人走到i+1。
即 dp[i][j]−>dp[i+1][j] d p [ i ] [ j ] − > d p [ i + 1 ] [ j ] 或者 dp[i][j]−>dp[i+1][i](j走到i+1后比i大,由于问题限制所以写成dp[i+1][i]) d p [ i ] [ j ] − > d p [ i + 1 ] [ i ] ( j 走 到 i + 1 后 比 i 大 , 由 于 问 题 限 制 所 以 写 成 d p [ i + 1 ] [ i ] )
由于设置状态表示两人分别走到i和j时,还需要走的距离最短是多少,那么需要将状态逆向推导,那么每次进行决策时,考虑的问题就是每次寻找下一步最优的方案是这也是该解法简化了问题的一个比较巧妙地方。
倘若不使用你想推导的方案,而采用顺序推导的方案。
那么,同样设置状态 dp[i][j] d p [ i ] [ j ] 表示两个人分别走到i和j时最短距离,那么在状态转移时,要考虑的是从哪一个状态转移到 dp[i][j] d p [ i ] [ j ] ? 如果利用紫书中提供的思路,会变成如下这样。
一种第一个人走到的i点,那么上一个状态应该是 dp[i−1][j] d p [ i − 1 ] [ j ] ,另一种是第二个人走到i点,上一个状态是 dp[i−j][j] d p [ i − j ] [ j ] ,这两个都有问题,i-1和j哪个大?i-j和j哪个大?所以,正向推导的问题不能这样考虑。
这里,如下考虑
定义
dp[i][k]
d
p
[
i
]
[
k
]
表示第一个人走到第 i 个点第二个人走到第 k 个点的最短距离(走在前面的人是 i )
对于任意一个点i来说,有两种连接方法,i 与 i-1相连或者 i 与 i-1 前面的点 k 相连
转移方程如下:
dp[i][k]=dp[i−1][k]+dist[i][i−1]; d p [ i ] [ k ] = d p [ i − 1 ] [ k ] + d i s t [ i ] [ i − 1 ] ;
dp[i][i−1]=min(dp[i][i−1],dp[i−1][k]+dist[k][i]); d p [ i ] [ i − 1 ] = m i n ( d p [ i ] [ i − 1 ] , d p [ i − 1 ] [ k ] + d i s t [ k ] [ i ] ) ;
dist[i][j] d i s t [ i ] [ j ] 表示i到j的距离
以上….