您的位置:首页 > 其它

hdu 2224 The shortest path

2016-05-11 16:44 381 查看

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


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

3

1 1

2 3

3 1

[align=left]Sample Output[/align]

6.47

Hint: The way 1 - 3 - 2 - 1 makes the shortest path.

[align=left]Author[/align]
8600

[align=left]Recommend[/align]
lcy | We have carefully selected several similar
problems for you: 2807 1142 1385 1595 1596

新学到了一个算法,双调欧几里得旅行商问题,此题可作为模板题看,详见博客: http://www.cnblogs.com/pshw/p/5482438.html

题意:给你几个点的坐标,从最左边搜到最右边,再从最右别搜到最左边,必须搜完所有的点,除了起始点 其余点不能重复,求最短的距离是多少。

附上代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#define inf 0x3f3f3f3f
#define M 205
using namespace std;
int n;
struct node
{
double x,y;
} ss[M];
double dis[M][M];
double distant(int i,int j)
{
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));
}

double dp()
{
int i,j;
double temp,b[M][M];
b[2][1]=dis[2][1];
for(i=3; i<=n; i++) //参考欧几里得旅行商算法
{
for(j=1; j<=i-2; j++)
b[i][j]=b[i-1][j]+dis[i-1][i];
b[i][i-1]=inf;
for(j=1; j<=i-2; j++)
{
temp=b[i-1][j]+dis[i][j];
if(temp<b[i][i-1])
b[i][i-1]=temp;
}
}
b

=b
[n-1]+dis
[n-1];
return b

;
}
int main()
{
int T,i,j,m;
double ans;
while(~scanf("%d",&n))
{
for(i=1; i<=n; i++)
scanf("%lf%lf",&ss[i].x,&ss[i].y);
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
dis[i][j]=distant(i,j); //记录每个点之间的距离
ans=dp();
printf("%.2lf\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: