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): 1225    Accepted Submission(s): 638


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 1142 1385 1595 1596 
 
新学到了一个算法,双调欧几里得旅行商问题,此题可作为模板题看,详见博客: http://www.cnblogs.com/pshw/p/5482438.html
 
题意:给你几个点的坐标,从最左边搜到最右边,再从最右别搜到最左边,必须搜完所有的点,除了起始点 其余点不能重复,求最短的距离是多少。
 
附上代码:
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #define inf 0x3f3f3f3f
 6 #define M 205
 7 using namespace std;
 8 int n;
 9 struct node
10 {
11     double x,y;
12 } ss[M];
13 double dis[M][M];
14 double distant(int i,int j)
15 {
16     return sqrt((ss[i].x-ss[j].x)*(ss[i].x-ss[j].x)+(ss[i].y-ss[j].y)*(ss[i].y-ss[j].y));
17 }
18 
19 double dp()
20 {
21     int i,j;
22     double temp,b[M][M];
23     b[2][1]=dis[2][1];
24     for(i=3; i<=n; i++) //参考欧几里得旅行商算法
25     {
26         for(j=1; j<=i-2; j++)
27             b[i][j]=b[i-1][j]+dis[i-1][i];
28         b[i][i-1]=inf;
29         for(j=1; j<=i-2; j++)
30         {
31             temp=b[i-1][j]+dis[i][j];
32             if(temp<b[i][i-1])
33                 b[i][i-1]=temp;
34         }
35     }
36     b[n][n]=b[n][n-1]+dis[n][n-1];
37     return b[n][n];
38 }
39 int main()
40 {
41     int T,i,j,m;
42     double ans;
43     while(~scanf("%d",&n))
44     {
45         for(i=1; i<=n; i++)
46             scanf("%lf%lf",&ss[i].x,&ss[i].y);
47         for(i=1; i<=n; i++)
48             for(j=1; j<=n; j++)
49                 dis[i][j]=distant(i,j); //记录每个点之间的距离
50         ans=dp();
51         printf("%.2lf\n",ans);
52     }
53     return 0;
54 }

 

posted @ 2016-05-11 16:44  lucky_少哖  阅读(203)  评论(0编辑  收藏  举报