The shortest path
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1050 Accepted Submission(s): 535
Before you reach the rightmost point Pn, you can only visit the points those have the bigger x-coordinate value. For example, you are at Pi now, then you can only visit Pj(j > i). When you reach Pn, the rule is changed, from now on you can only visit the points those have the smaller x-coordinate value than the point you are in now, for example, you are at Pi now, then you can only visit Pj(j < i). And in the end you back to P1 and the tour is over.
You should visit all points in this tour and you can visit every point only once.
3 1 1 2 3 3 1
6.47 Hint: The way 1 - 3 - 2 - 1 makes the shortest path.
题目大意:有n个点,告诉每个点的坐标。先从p1到pn,pi到pj必须保证i<j。再从pn到p1,pi到pj必须保证i>j,同时,来回之后必须保证每个点到且仅到一次。求最短路程。
对于题目来说,可以看做是两条从1出发到n的路。每当一条路选定了走法,另一条路的走法就是唯一的了。
两条路径分别为A和B,且起点都是点1,方向严格向右
1、A[i]表示路径A的一种状态,起点为点1,终点为点i,方向严格向右,1<=i<=n
2、B[j]表示路径B的一种状态,起点为点1,终点为点i,方向严格向右,1<=j<=n
3、dist[i][j]为点i与点j之间的距离
4、dp[i][j]为满足以下条件的A[i]和B[j]的路径和的最小长度:
a)A[i]和B[j]包含了所有的1-max(i,j)中的点;
b)A[i]和B[j]中没有重复了点(除了点1,或i=j时的点i)
经过以上定义,题目可以化简为求dp[n][n]的值。
dp[i][j]的意义即是A走到i点,B走到j点时的最短路程
min(dp[i][k]+dist[k][j])(1<=k<i) j=i+1;
dp[i][j] =
dp[i][j-1]+dist[j][j-1] j>i+1;
dp[i][i] = dp[i][i-1]+dist[i][i-1];
转:http://blog.csdn.net/catalyst1314/article/details/19005845
ac代码
15289251 | 2015-10-28 23:55:41 | Accepted | 2224 | 15MS | 2412K | 824 B | C++ | Who_you? |
#include<stdio.h>
#include<string.h>
#include<math.h>
#define INF 0x3f3f3f3f
double dp[220][220],dis[220][220];
struct s
{
double x,y;
}a[220];
double distan(int i,int j)
{
return sqrt((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y));
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int i,j;
for(i=1;i<=n;i++)
scanf("%lf%lf",&a[i].x,&a[i].y);
for(i=2;i<=n;i++)
for(j=1;j<i;j++)
{
dis[j][i]=distan(j,i);
}
dp[1][2]=dis[1][2];
for(i=3;i<=n;i++)
{
for(j=1;j<=i-2;j++)
{
dp[j][i]=dp[j][i-1]+dis[i-1][i];
}
dp[i-1][i]=INF;
for(int k=1;k<=i-2;k++)
{
double temp=dp[k][i-1]+dis[k][i];
if(temp<dp[i-1][i])
dp[i-1][i]=temp;
}
}
dp[n][n]=dp[n-1][n]+dis[n-1][n];
printf("%.2lf\n",dp[n][n]);
}
}