程序设计实习2016推荐练习题 Tour(dp/记忆化搜索)
总时间限制: 1000ms 内存限制: 65536kB
描述
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 pi = < xi,yi >. 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.
输入
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.
The number of points of each data set is at most 50, and each coordinate does not exceed 20000 by an absolute value.
输出
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. 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).
样例输入
3
1 1
2 3
3 1
4
1 1
2 3
3 1
4 2
样例输出
6.47
7.89
以前学习过这个题目,查了博客知道了这个叫双调欧几里得旅行商问题(双调TSP),虽然TSP是NP的,但是加了双调这个条件就弱了一点,存在dp算法,归根结底是因为如果你考虑两个点发现它们要么在一条线上,要么在两条线上,而一共只有两条线,所以取定了两个不同一条线路上的点相当于局部确定了一大堆线路,而这些线路的最短长度是一个反复被搜索到的子问题。
以前学过所以第一遍写的时候总在回忆没有自己思考,写不出来,后来我意识到dp没什么难的,要相信自己,不要为以前的定式所拘泥,自己分析了一下问题到底要写什么就写出来了。
但是犯了小错误,没考虑输入怎么结束,结果读不完,TLE…所以以后最好写
while((scanf("%d",&n)==1)&&n)
这样不同格式都可以读,不管有没有0结尾,万一看错格式说不定也能救你一命,当然,读题要仔细。
最近做到题目让我有一些小体会,对数据范围绷紧一根弦来判断复杂度进而判断是不是dp,是什么复杂度的dp固然是一个好的技巧,牢记定式固然提高了一点码代码速度,但是过分依赖甚至以此代替了自己思考,代码能力不会提高,而且由于常数不可控,往往出错。所以自己一定要学会独立思考,这一点我做的不够好,而且似乎积累不够多,加上算法天赋也不是突出,所以很吃力,但是还是要努力。
Accepted 260kB 0ms 1337 B G++
#define MAX_N 50
#include<stdio.h>
#include<memory.h>
#include<stdlib.h>
#include<math.h>
int n;
double p[MAX_N+1][2];
double dp[MAX_N+1][MAX_N+1];
inline int compare(const void* e1,const void* e2)
{
return *(double*)e1-*(double*)e2;
}
inline double dis(double p1[2],double p2[2])
{
return sqrt((p1[1]-p2[1])*(p1[1]-p2[1])+(p1[0]-p2[0])*(p1[0]-p2[0]));
}
double f(int i,int j)
{
int temp;
if (dp[i][j])
return dp[i][j];
if (j<i+1)
{
temp=i;
i=j;
j=temp;
}
if (i<j-1)
return dp[i][j]=f(i,j-1)+dis(p[j-1],p[j]);
if (i==j-1)
{
dp[i][j]=f(1,i)+dis(p[1],p[j]);
for (int t=2;t<=i-1;t++)
if (f(t,i)+dis(p[t],p[j])<dp[i][j])
dp[i][j]=f(t,i)+dis(p[t],p[j]);
return dp[i][j];
}
if (i==n && j==n)
{
dp[n][n]=f(1,n)+dis(p[1],p[n]);
for (int t=2;t<n;t++)
if (f(t,n)+dis(p[t],p[n])<dp[n][n])
dp[n][n]=f(t,n)+dis(p[t],p[n]);
return dp[n][n];
}
}
int main()
{
//freopen("input.txt","r",stdin);
while ((scanf("%d",&n)==1)&&n)
{
memset(dp,0,sizeof(dp));
for (int i=1;i<=n;i++)
scanf("%lf%lf",&p[i][0],&p[i][1]);
qsort(p+1,n,sizeof(p[0]),compare);
dp[1][1]=0;
for (int i=2;i<=n;i++)
dp[1][i]=dp[i][1]=dp[i-1][1]+dis(p[i-1],p[i]);
printf("%.2lf\n",f(n,n));
/*
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
printf("%.2lf%c",dp[i][j],j==n?'\n':' ');
*/
}
return 0;
}