HDOJ 题目2224 The shortest path||POJ 题目2677 Tour(双调TSP)

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


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.
 

Author
8600
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   2807  3339  1245  2433  1482 
 

题目大意:有n个点,告诉每个点的坐标。先从p1pnpipj必须保证i<j。再从pnp1pipj必须保证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代码

152892512015-10-28 23:55:41Accepted222415MS2412K824 BC++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]);
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值