Tour UVA - 1347
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.
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.
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 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
3
1 1
2 3
3 1
4
1 1
2 3
3 1
4 2
Sample Output
6.47
7.89
,
这题就是DP,思路什么的书上说的很清楚了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int n;
struct node
{
double x,y;
}a[];
double dis[][];
double dp[][];
int main()
{
while(scanf("%d",&n)==)
{
for(int i=;i<=n;i++)
{
scanf("%lf%lf",&a[i].x,&a[i].y);
for(int j=i-;j>=;j--)
dis[j][i]=sqrt(((a[i].x-a[j].x)*(a[i].x-a[j].x))+((a[i].y-a[j].y)*(a[i].y-a[j].y)));
}
//pre();
for(int i=n-;i>=;i--)
dp[n-][i]=dis[n-][n]+dis[i][n];
for(int i=n-;i>=;i--)
for(int j=i-;j>=;j--)
dp[i][j]=min(dp[i+][j]+dis[i][i+],dp[i+][i]+dis[j][i+]);
printf("%.2lf\n",dp[][]+dis[][]);
}
return ;
}
Tour UVA - 1347的更多相关文章
- ACM - 动态规划 - UVA 1347 Tour
UVA 1347 Tour 题解 题目大意:有 \(n\) 个点,给出点的 \(x\).\(y\) 坐标.找出一条经过所有点一次的回路,从最左边的点出发,严格向右走,到达最右点再严格向左,回到最左点. ...
- UVa 1347 Tour
Tour Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Description Joh ...
- 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
[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坐标从小到大一次给出.求一条最短路线,从最左边的点出发到最右边的点,再回到最左边的点.除了第一个和最右一个点其他点恰好只经过一次. 分析: 可以等效为两个人 ...
- 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 ...
随机推荐
- 第七节:numpy之矩阵及特殊矩阵的创建
- 【Codeforces 1114C】Trailing Loves (or L'oeufs?)
[链接] 我是链接,点我呀:) [题意] 问你n!的b进制下末尾的0的个数 [题解] 证明:https://blog.csdn.net/qq_40679299/article/details/8116 ...
- 洛谷P3375【模板】KMP字符串匹配
题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next. (如果你不知道这是什么意思也不要问,去百度 ...
- hdu 4950
#include<stdio.h> int main(){ __int64 h,a,b,k,j=0; while(scanf("%I64d%I64d%I64d%I64d" ...
- ACM的你伤不起
劳资六年前开始搞ACM啊!!!!!!!!!! 从此踏上了尼玛不归路啊!!!!!!!!!!!! 谁特么跟劳资讲算法是程序设计的核心啊!!!!!! 尼玛除了面试题就没见过用算法的地方啊!!!!!! ...
- CF #321 (Div. 2) E
用线段树维护哈希,类似于进位制的一个哈希 a[i]*p^i+a[i-1]*p^i-1... 然后,线段树存在某区间的哈希的值,对于更新,则只需提前计算出整段的哈希值即可. 判断是否相等,由于相隔为d, ...
- HDU 5434
其实是一道状态DP题.都是行与行之间的转移,可以知道,当某j列中有一个象,如果存在情况i-1行j-1列有象而i,j-1位置无象则不可放,或者i-1,j+1有而i,j+1无同样不可放. 使用快速状态转移 ...
- SQL Server高速导入数据分享
SQL Server高速导入数据,能够尝试的方法例如以下:CTE.OpenRowSet/OpenDataSource.BULK INSERT.bcp.Shell. 以下依次介绍这几种办法. 1.CTE ...
- 我的Android进阶之旅------>Android中ListView中嵌套(ListView)控件时item的点击事件不起作的问题解决方法
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- 使用Fiddler进行抓包
参考:http://blog.csdn.net/gld824125233/article/details/52588275 1.手机跟电脑需要使用同一个局域网 如果是台式机需要将手机的mac地址让运 ...