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; }
相关文章推荐
- C#_串口通信_SerialPort_一个最基础的串口程序
- epoll 分析----2
- UIImage两种初始化的区别
- Oracle AWR报告的生成
- 04_Swift2基础之类型安全和类型推测+字面量+类型别名
- android彻底退出问题
- 02_Swift2基础之常量和变量+注释+分号
- Java - HashSet
- iOSDay30之UITableView编辑
- 在 Linux 上安装 Git
- ROS 学习系列 -- iRobot 第二代机座 Roomba 作为Turtlebot使用时无法开关机
- Object-c 开源类库
- 计算机网络之面试常考
- iOS 仿Android Toast效果
- 如何解决PC端和移动端自适应问题?
- APUE,TLPI读书笔记——信号
- 键盘回收
- C++中的vector容器对象学习笔记
- PX4Firmware中的.mk的调用关系和作用
- Dubbo-admin 不支持JDK8 解决办法