hdu 2224 The shortest path 题解(动态规划)

点击打开链接

The shortest path

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 746    Accepted Submission(s): 383


Problem Description
There are n points on the plane, Pi(xi, yi)(1 <= i <= n), and xi < xj (i<j). You begin at P1 and visit all points then back to P1. But there is a constraint:
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.
 

Input
The input consists of multiple test cases. Each case begins with a line containing a positive integer n(2 <= n <= 200), means the number of points. Then following n lines each containing two positive integers Pi(xi, yi), indicating the coordinate of the i-th point in the plane.
 

Output
For each test case, output one line containing the shortest path to visit all the points with the rule mentioned above.The answer should accurate up to 2 decimal places.
 

Sample Input
  
  
3 1 1 2 3 3 1
 

Sample Output
  
  
6.47 Hint: The way 1 - 3 - 2 - 1 makes the shortest path.
题意:给N个二维平面上面的点,要从第一个点走到第N个点(若从i到j,必须i<j),再从第N个点走回第一个点(若从i到j,必须i>j),每个点只能访问且必须访问一次,求最短路径。
我们把从第一个点到第N个点的路径看成路径0,第N个点到第一个点的路径看成路径1,那么除开第一个点和第N个点,其余每个点只有两种策略,要么在路径0,要么在路径1。
我们从第N个点开始选,依次选点N-1,N-2.....1,这样定义状态 dp[i][x][y],表示n到i已经选过,路径0以x结束,路径1以y结束的最短路径(除开第N个点其余点只能在一条路径上),显然x和y其中一个一定是i,所以可以将空间优化到两维,dp[i][0][j]表示i在路径0,路径1以j结束的最短路径,dp[i][1][j]则表示i在路径1,路径0以i结束的最短路径。很容易写出转移方程:dp[i][0][j]=max(dp[i][0][j],dp[i+1][0][j]+dis(i+1,i)),i+1<=j<=n;dp[i][0][i+1]=max(dp[i][0][i+1],dp[i+1][1][j]+dis(i,j)),i+1<=j<=n;同理可得dp[i][1][j],dp[i][1][j+1]的转移方程,具体代码如下:
#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<math.h>
#define nn 110
#define inff 0x3fffffff
#define mod 1000000007
#define eps 1e-9
using namespace std;
typedef long long LL;
struct node
{
    int x,y;
}a[2*nn];
int n;
double dp[2*nn][2][2*nn];
double dis(int i,int j)
{
    double f1=(a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y);
    return sqrt(f1);
}
int main()
{
    int i,j,k;
    while(scanf("%d",&n)!=EOF)
    {
        for(i=1;i<=n;i++)
        {
            scanf("%d%d",&a[i].x,&a[i].y);
        }
        for(i=1;i<=n;i++)
        {
            for(j=0;j<=1;j++)
            {
                for(k=1;k<=n;k++)
                {
                    dp[i][j][k]=1e20;
                }
            }
        }
        dp[n][0][n]=0;
        dp[n][1][n]=0;
        for(i=n-1;i>=0;i--)
        {
            for(j=i+1;j<=n;j++)
            {
                dp[i][0][j]=min(dp[i][0][j],dp[i+1][0][j]+dis(i,i+1));
                dp[i][1][j]=min(dp[i][1][j],dp[i+1][1][j]+dis(i,i+1));
            }
            for(j=i+1;j<=n;j++)
            {
                dp[i][0][i+1]=min(dp[i][0][i+1],dp[i+1][1][j]+dis(i,j));
                dp[i][1][i+1]=min(dp[i][1][i+1],dp[i+1][0][j]+dis(i,j));
            }
        }
        double ans=1e20;
        for(i=1;i<=n;i++)
        {
            ans=min(ans,dp[1][0][i]+dis(1,i));
            ans=min(ans,dp[1][1][i]+dis(1,i));
        }
        printf("%.2lf\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值