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.

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

3
1 1
2 3
3 1
4
1 1
2 3
3 1
4 2

Sample Output

6.47
7.89

算是经典问题了吧。题目大意:给定平面上n个点的坐标(按x的升序),

你的任务是设计一条路线从最左边出发,到最右边再返回最左边,每点恰好经过一次。问最小代价。能够看成两个人从最左端同一时候出发。

每点恰有一人经过的最小代价。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
#include<cmath>
typedef long long LL;
using namespace std;
const int maxn=110;
double x[maxn],y[maxn];
double dp[maxn][maxn]; double dis(int i,int j)
{
return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
} int main()
{
int n;
while(~scanf("%d",&n)&&n)
{
for(int i=0;i<n;i++)
scanf("%lf%lf",&x[i],&y[i]);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
dp[i][j]=INT_MAX;
}
dp[0][0]=0.0;//初始化
for(int i=0;i<n;i++)
{
for(int j=0;j<i;j++)
{
dp[i][i-1]=min(dp[i][i-1],dp[i-1][j]+dis(i,j));//当走到dp[i-1][j]要走到第i个点时。有两种途径
dp[i][j]=min(dp[i][j],dp[i-1][j]+dis(i-1,i));//在i-1的人走到i或在j的人走到i;
}
}
// for(int i=0;i<n;i++)
// printf("fuck %d\n",dp[n-1][i]);
double ans=dp[n-1][n-2]+dis(n-1,n-2);
printf("%.2f\n",ans);
}
return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

UVA 1347(POJ 2677) Tour(双色欧几里德旅行商问题)的更多相关文章

  1. POJ 2677 Tour

    题意:双调欧几里得旅行商问题.算法导论15-1题,从最左边的点严格从左走到右再从右走到左回到起点,所有点都要走且只走一次,求最短路径. 解法:定义dp[i][j]表示从i走到j的双调路径,分为两种情况 ...

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

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

  3. 【玩转单片机系列001】 08接口双色LED显示屏驱动方式探索

    前些日子,从淘宝上购得一块08接口的双色LED显示屏(打算做个音乐频谱显示器),捣鼓了好几天,终于搞清楚了其控制原理,在这里做个总结,算是备忘吧. 1.LED显示屏的扫描方式 LED显示屏的扫描方式有 ...

  4. 完美C++(第5版)(双色)

    完美C++(第5版)(双色) 薛正华 沈庚 韦远科 译 ISBN 978-7-121-23198-8 2014年6月出版 定价:148.00元 788页 16开 内容提要 <完美C++(第5版) ...

  5. HTML5移动开发即学即用(双色) 王志刚 pdf扫描版​

    HTML5已经广泛应用于各智能移动终端设备上,而且绝大部分技术已经被各种最新版本的测览器所支持:逐一剖析HTML5标准中包含的最新技术,详细介绍了HTML5新标准中提供的各种API,各种各样的应用实例 ...

  6. 括号序列问题 uva 1626 poj 1141【区间dp】

    首先考虑下面的问题:Code[VS] 3657 我们用以下规则定义一个合法的括号序列: (1)空序列是合法的 (2)假如S是一个合法的序列,则 (S) 和[S]都是合法的 (3)假如A 和 B 都是合 ...

  7. css实现双色饼图

    from:wx--前端早读课 首先回想用css画三角形的方法: <div class="triangle"></div> .triangle { displ ...

  8. UVa 1347 (双线程DP) Tour

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

  9. UVa 1347 Tour

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

随机推荐

  1. 4、qq物联SDK介绍及实例讲解

    1.到QQ物联官网http://iot.open.qq.com中下载软件SDK S3C2440_20161122_1.6.205_r4288.tar.gz注意:在后续大家实际开发过程中,可能你会下载到 ...

  2. ITFriend月刊-第1期-2014年6月.pdf

    ITFriend上线一个月了,积累了不少优质内容,本周进行了整理,制作了PDF格式的电子书. 欢迎大家下载阅读. 下载地址: CSDN下载:http://download.csdn.net/detai ...

  3. php课程 2-7 php中常量如何定义

    php课程 2-7 php中常量如何定义 一.总结 一句话总结:函数方式定义和普通变量方式定义    define('PI','3.14').键值对,和session一样.    const PI=3 ...

  4. go 保留小数若干位数

    感谢 https://blog.csdn.net/sjy8207380/article/details/79013827 解决的方法 · 利用取近似值的方法解决这个问题. (1)利用fmt.Sprin ...

  5. 修改Linux中的用户名 分类: B3_LINUX 2014-07-24 11:40 440人阅读 评论(0) 收藏

    需要修改2个文件: /etc/hosts /etc/sysconfig/network 然后重启 1.修改/etc/sysconfig/network NETWORKING=yes HOSTNAME= ...

  6. 并发队列ConcurrentLinkedQueue 和 阻塞队列LinkedBlockingQueue用法

    在Java多线程应用中,队列的使用率很高,多数生产消费模型的首选数据结构就是队列(先进先出).Java提供的线程安全的Queue可以分为阻塞队列和非阻塞队列,其中阻塞队列的典型例子是BlockingQ ...

  7. 小梦词典WP8.1应用发布

    这几天一直在做这款应用,今天终于发布了! 小梦词典简介: 小梦词典是一款永久免费无广告的网络词典. 支持英汉单词查询: 支持中,英,法,韩,德,俄,日七国语言翻译,多语言极致体验: 支持生词本记忆,查 ...

  8. 【心情】bjdldrz

    如果我对勇士的记忆停留在73胜10负,

  9. 【C++竞赛 F】yyy的三角形

    时间限制:2s 内存限制:32MB 问题描述 yyy对三角形非常感兴趣,他有n个木棍,他正在用这些木棍组成三角形.这时xxx拿了两根木棍过来,xxx希望yyy能给他一根木棍,使得xxx可以组成一个三角 ...

  10. Android SqlDelight具体解释和Demo样例

    一.简单介绍 SQLDelight 和 SqlBrite 是 Square 公司推出的一个 Android 平台数据库解决方式. 在了解这个两个东西前,必须先得有Andorid的Sqlite的知识(S ...