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的更多相关文章

  1. ACM - 动态规划 - UVA 1347 Tour

    UVA 1347 Tour 题解 题目大意:有 \(n\) 个点,给出点的 \(x\).\(y\) 坐标.找出一条经过所有点一次的回路,从最左边的点出发,严格向右走,到达最右点再严格向左,回到最左点. ...

  2. UVa 1347 Tour

    Tour Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Description   Joh ...

  3. UVA 1347 Tour 【双调旅行商/DP】

    John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane and starts vi ...

  4. 【UVa 1347】Tour

    [Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  5. UVA 1347(POJ 2677) Tour(双色欧几里德旅行商问题)

    Description John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane a ...

  6. UVa 1347 (双线程DP) Tour

    题意: 平面上有n个坐标均为正数的点,按照x坐标从小到大一次给出.求一条最短路线,从最左边的点出发到最右边的点,再回到最左边的点.除了第一个和最右一个点其他点恰好只经过一次. 分析: 可以等效为两个人 ...

  7. UVA - 1347 Tour(DP + 双调旅行商问题)

    题意:给出按照x坐标排序的n个点,让我们求出从最左端点到最右短点然后再回来,并且经过所有点且只经过一次的最短路径. 分析:这个题目刘汝佳的算法书上也有详解(就在基础dp那一段),具体思路如下:按照题目 ...

  8. UVA 1347 Tour 双调TSP

    TSP是NP难,但是把问题简化,到最右点之前的巡游路线只能严格向右,到最右边的点以后,返回的时候严格向左,这个问题就可以在多项式时间内求出来了. 定义状态d[i][j]表示一个人在i号点,令一个人在j ...

  9. UVA 1347"Tour"(经典DP)

    传送门 参考资料: [1]:紫书 题意: 欧几里得距离???? 题解: AC代码: #include<bits/stdc++.h> using namespace std; ; int n ...

随机推荐

  1. enote笔记语言(4)(ver0.2)——“5w1h2k”分析法

    章节:“5w1h2k”分析法   what:我想知道某个“关键词(keyword)”(即,词汇.词语,或称单词,可以是概念|专业术语|.......)的定义. why:我想知道事物发生的原因.“why ...

  2. SDOI2018退役记

    在NOIp2017中,我意识到自己啥也不会.如今SDOI2018快来了,自己还是啥也不会.高一两次考试注定以打两次酱油告终.还是记录一下,到NOIp之后如果还没有退役的话,那这个博客可能还会继续更新吧 ...

  3. yum更换国内源及yum下载rpm包

    一.yum更换国内源 运维开发技术交流群欢迎大家加入一起学习(QQ:722381733) 1.前往yum文件路径地址 [root@web1 ~]# cd /etc/yum.repos.d/ [root ...

  4. System----堡垒机

    你知道嘛是堡垒机吗? 你知道堡垒机是奏嘛的吗? 1,改server 端 socket server 接受到的请求 执行指令前,记录收到的指令,来源ip 用户名 缺点:每台机器都要更改源码,加入指令记录 ...

  5. CodeForcesGym 100735H Words from cubes

    Words from cubes Time Limit: Unknown ms Memory Limit: 65536KB This problem will be judged on CodeFor ...

  6. elasticsearch match和match_phrase匹配原则(十)

    分词测试 分词后倒排索引结果:可以通过http://127.0.0.1:9200/_analyze 测试 { "analyzer":"ik_max_word", ...

  7. 同开三本DJANGO,需要提升一下本职工作的能力啦

    慢慢将一些概念固化到基因内,才有可能和SPRING MVC,MEAN之类的好好作比较吧. 全都是基于1.8版本的教材,爽!!!

  8. nyoj_91_阶乘之和_201312131321

    阶乘之和 时间限制:3000 ms  |           内存限制:65535 KB 难度:3   描述 给你一个非负数整数n,判断n是不是一些数(这些数不允许重复使用,且为正数)的阶乘之和,如9 ...

  9. 【ACM】NYOJ_486_Old Calculator_20130725

    Old Calculator时间限制:1000 ms  |  内存限制:65535 KB 难度:1描述 szhhck have an old calculator bought 5 years ago ...

  10. Android: Could not find com.android.support:support-v4:21.0.0

    安装Android Support Repository即可解决这个问题.