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. 关于linux内核用纯c语言编写的思考

    在阅读linux2.6 版本内核的虚拟文件系统和驱动子系统的时候,我发现内核纯用c语言编写其实也是有一点不方便,特别是内核中大量存在了对象的概念,比如说文件对象,描述起来使用对象描述,但是对象在c语言 ...

  2. 费用最少的一款赛门铁克SSL证书

    Symantec Secure Site SSL证书,验证域名所有权和企业信息,属于Symantec Class 3企业(OV)验证 级SSL证书,为40位/56位/128/256位自适应加密,目前连 ...

  3. 【codeforces 510A】Fox And Snake

    [题目链接]:http://codeforces.com/contest/510/problem/A [题意] 让你画一条蛇.. [题解] 煞笔提 [Number Of WA] 0 [完整代码] #i ...

  4. mysql :=和=的区别

    :=和=的区别 = 只有在set和update时才是和:=一样,赋值的作用,其它都是等于的作用.鉴于此,用变量实现行号时,必须用:= := 不只在set和update时时赋值的作用,在select也是 ...

  5. R语言为数据框添加列名或行名

    1.添加列名 wts=c(1,1,1) names(wts)=c("setosa","versicolor","virginica") 2. ...

  6. axis实现webservices分布式通信

    分布式通信原理 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2ZsMjAxMjEzMTQ=/font/5a6L5L2T/fontsize/400/fil ...

  7. Oracle学习(12):存储过程,函数和触发器

    存储过程和存储函数 l存储在数据库中供全部用户程序调用的子程序叫存储过程.存储函数. 注意:存储过程与存储函数声明变量时,用的是as   而不是declare 存储过程与存储函数差别 存储过程不带有返 ...

  8. NoSQL数据库:Redis内存使用优化与存储

    Redis常用数据类型 Redis最为常用的数据类型主要有以下五种: ●String ●Hash ●List ●Set ●Sorted set 在具体描述这几种数据类型之前,我们先通过一张图了解下Re ...

  9. XMPP 协议工作流程具体解释

    XMPP 要点. 1. client(C) 和server端(S) 通过TCP连接5222port进行全双工通信. 2. XMPP 信息均包括在 XML streams中.一个XMPP会话, 開始于& ...

  10. android中的back键处理

    Back键是手机上的后退键,一般的软件不捕获相关信息可能导致你的程序被切换到后台,而回到桌面的尴尬情况,在Android上有两种方法来获取该按钮的事件. 1.直接获取按钮按下事件,此方法兼容Andro ...