UVa 1347 Tour
| Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
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 ><tex2html_verbatim_mark> . 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<tex2html_verbatim_mark> -coordinates.
Write a program that, given a set of n<tex2html_verbatim_mark> 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<tex2html_verbatim_mark> 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.
Note: 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<tex2html_verbatim_mark> and y<tex2html_verbatim_mark>coordinates. The second point, for example, has the x<tex2html_verbatim_mark> coordinate 2, and the y<tex2html_verbatim_mark> 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
3
1 1
2 3
3 1
4
1 1
2 3
3 1
4 2
Sample Output
6.47
7.89
可以把一个人的往返看作是两个人从起点同时出发走到终点,两人走到的点不重复
则对于一个点,要么一个人过去,要么另一个人过去(不需要区分到底是哪个人)
/*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int mxn=;
int x[mxn],y[mxn];
double mp[mxn][mxn];
double f[mxn][mxn];
int n;
int main(){
while(scanf("%d",&n)!=EOF){
memset(f,,sizeof(f));
int i,j;
for(i=;i<=n;i++){
scanf("%d%d",&x[i],&y[i]);
}
for(i=;i<=n;i++){
for(j=;j<=n;j++){
mp[i][j]=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
}
}
for(i=n-;i>=;i--){//i倒推
for(j=;j<i;j++){
if(i==n-)f[i][j]=mp[i][n]+mp[j][n];//边界
else f[i][j]=min(mp[i][i+]+f[i+][j],mp[j][i+]+f[i+][i]);
}
}
printf("%.2f\n",mp[][]+f[][]);
//最终状态是一个人走到了点2,一个人走到了点1,需要再加上从2到1的距离
}
return ;
}
UVa 1347 Tour的更多相关文章
- ACM - 动态规划 - UVA 1347 Tour
UVA 1347 Tour 题解 题目大意:有 \(n\) 个点,给出点的 \(x\).\(y\) 坐标.找出一条经过所有点一次的回路,从最左边的点出发,严格向右走,到达最右点再严格向左,回到最左点. ...
- UVA 1347 Tour 【双调旅行商/DP】
John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane and starts vi ...
- UVA - 1347 Tour(DP + 双调旅行商问题)
题意:给出按照x坐标排序的n个点,让我们求出从最左端点到最右短点然后再回来,并且经过所有点且只经过一次的最短路径. 分析:这个题目刘汝佳的算法书上也有详解(就在基础dp那一段),具体思路如下:按照题目 ...
- UVA 1347 Tour 双调TSP
TSP是NP难,但是把问题简化,到最右点之前的巡游路线只能严格向右,到最右边的点以后,返回的时候严格向左,这个问题就可以在多项式时间内求出来了. 定义状态d[i][j]表示一个人在i号点,令一个人在j ...
- UVA 1347"Tour"(经典DP)
传送门 参考资料: [1]:紫书 题意: 欧几里得距离???? 题解: AC代码: #include<bits/stdc++.h> using namespace std; ; int n ...
- Tour UVA - 1347
John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane and starts vi ...
- 【UVa 1347】Tour
[Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- UVA 1347(POJ 2677) Tour(双色欧几里德旅行商问题)
Description John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane a ...
- UVa 1347 (双线程DP) Tour
题意: 平面上有n个坐标均为正数的点,按照x坐标从小到大一次给出.求一条最短路线,从最左边的点出发到最右边的点,再回到最左边的点.除了第一个和最右一个点其他点恰好只经过一次. 分析: 可以等效为两个人 ...
随机推荐
- RecyclerView (一) 基础知识
RecyclerView是什么? RecyclerView是一种新的视图组,目标是为任何基于适配器的视图提供相似的渲染方式.它被作为ListView和GridView控件的继承者,在最新的suppor ...
- Android优化——UI检视利器:Hierarchy Viewer
在Android的SDK工具包中,有很多十分有用的工具,可以帮助程序员开发和测试Android应用程序,大大提高其工作效率.其中的一款叫 Hierachy Viewer的可视化调试工具,可以很方便地在 ...
- Android开发eclipse运行程序时报timeout的解决方法
eclipse开发Android程序时,忽然莫名其妙报这个错,之前还好好的.忽然就不行了. Failed to install xxx.apk on device~~~: timeout 尝试过手机里 ...
- AD域的安装
AD域的安装 初始化设置,改计算机名字dcserver,改静态ip,改dns指向自己. dcpromo,执行后自动装了dns. 装完后检查 1,本地用户没了 2,dns指向自己 3,dns记录是否 ...
- .NET跨平台之OWEN中 过滤器的使用
.NET中依赖IIS,通俗的说就是依赖IIS的程序集,导致我们的.NET项目就算是MONO到TOMCAT上,也无法使用,所以OWEN横空出世,OWEN定义了一套接口,接口定义了做.NET项目要实现的一 ...
- WPF之TreeList的实现方法(一)
做项目的时候根据需求,WPF现有的控件不能完全满足我们的需求, 很多时候我们需要对现有的控件做一下加工. 最简单的我们可能会把Tree转换成List形式有的叫Grid形式就像下图一样 今天我先做一个完 ...
- 不得不说的JavaScript异步加载
同步加载的问题 默认的js是同步加载的,这里的“加载”可以理解成是解析.执行,而不是“下载”,在最新版本的浏览器中,浏览器对于代码请求的资源都是瀑布式的加载,而不是阻塞式的,但是js的执行总是阻塞的. ...
- EF 基本数据过滤
没猜错的话, 你们一定会和一大堆查询条件过不去, 重复的写,反复的写, 写到山崩地裂. 今天看了园友的文字:实体框架高级应用之动态过滤 EntityFramework DynamicFilters 我 ...
- TO~亲爱的自己
你累的时候,谢绝别人的肩膀: 你扛不动的时候,拒绝别人的帮忙: 你和别人吃饭,不让别人买单你总是想我这样优秀善良, 总不给别人添麻烦,为什么总是找不到爱的人呢? 有时,爱只有在相处时才能找得到的, 是 ...
- How to set China Azure Storage Connection String
Configure Visual Studio to access China Azure Storage Open Visual Studio 2012, Server Explorer Add n ...