POJ2677
来源:互联网 发布:堆芯熔化 知乎 编辑:程序博客网 时间:2024/05/17 06:29
Tour
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 3435 Accepted: 1528
Description
John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane and starts visiting beautiful places. To save money, John must determine the shortest closed tour that connects his destinations. Each destination is represented by a point in the plane pi = < xi,yi >. John uses the following strategy: he starts from the leftmost point, then he goes strictly left to right to the rightmost point, and then he goes strictly right back to the starting point. It is known that the points have distinct x-coordinates.
Write a program that, given a set of n points in the plane, computes the shortest closed tour that connects the points according to John's strategy.
Write a program that, given a set of n points in the plane, computes the shortest closed tour that connects the points according to John's strategy.
Input
The program input is from a text file. Each data set in the file stands for a particular set of points. For each set of points the data set contains the number of points, and the point coordinates in ascending order of the x coordinate. White spaces can occur freely in input. The input data are correct.
Output
For each set of data, your program should print the result to the standard output from the beginning of a line. The tour length, a floating-point number with two fractional digits, represents the result. An input/output sample is in the table below. Here there are two data sets. The first one contains 3 points specified by their x and y coordinates. The second point, for example, has the x coordinate 2, and the y coordinate 3. The result for each data set is the tour length, (6.47 for the first data set in the given example).
Sample Input
31 12 33 141 12 33 14 2
Sample Output
6.477.89
【题意】就是按照要求遍历每一个地点,使得遍历长度最短;
【题解】双调旅行商问题,具体这里有一个写得不错的博客:http://blog.csdn.net/xiajun07061225/article/details/8092247
#include<cstdio>#include<cstring>#include<cmath>#define inf 2147483641.0#define min(x,y) x<y?x:ystruct pp{double x,y;}a[50];double f[50][50];double fang(double p){return p*p;}double dist(int i,int j){return sqrt(fang(a[i].x-a[j].x)+fang(a[i].y-a[j].y));}int main(){int n;while(scanf("%d",&n)!=EOF){for(int i=1;i<=n;i++)scanf("%lf%lf",&a[i].x,&a[i].y);for(int i=1;i<=n;i++) for(int j=1;j<=n;j++)f[i][j]=inf;f[1][1]=0;for(int i=1;i<=n;i++) for(int j=1;j<=i;j++) if(j<(i-1))f[i][j]=f[i-1][j]+dist(i,i-1);else if(j==(i-1)) for(int k=1;k<=i-1;k++) f[i][j]=min(f[i][j],dist(i,k)+f[i-1][k]);else if(j==i)for(int k=1;k<=i-1;k++) f[i][j]=min(f[i][j],f[i][k]+dist(i,k));printf("%.2lf\n",f[n][n]);}return 0;}
0 0
- poj2677
- POJ2677
- POJ2677.Tour
- 原 poj2677(基础题)
- POJ2677--Tour DP
- ZOJ2581 POJ2677 Tour,双调DP
- UVa1347/poj2677 - C - Tour(DP)
- HDU2224&POJ2677 双调旅行商问题
- POJ2677,HDU2224【双调欧几里得模板题】
- POJ2677 Tour(DP:双调巡游)
- POJ2677 DP tour 双调欧几里得旅行商问题
- hdu2224——The shortest path && POJ2677——Tour
- 双调旅程(bitonic tour)问题(POJ2677、ZOJ2096)
- js瀑布式动态触发加载数据(手机微博,空间下拉动态显示效果)
- 医调项目之COOIKE操作
- 解决PhpCms V9后台无法上传图片
- ubuntu14.04搭建samba
- 开博第一篇 叩谢各位大神。
- POJ2677
- 在使用MFC双向链表模板类CList中Find问题
- Leetcode3: Max Points on a Line
- C语言文件操作
- MFC控件(三)(进度条控件和滑块控件)
- ubuntu的init与系统服务设置
- 终有一日,会睡醒
- iis7.5 配置 php5.5以上版本说明
- Weblogic服务器中配置消息服务器图解