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. BZOJ 2245 SDOI 2011 工作安排 费用流

    题目大意:有一些商品须要被制造.有一些员工.每个员工会做一些物品,然而这些员工做物品越多,他们的愤慨值越大,这满足一个分段函数.给出哪些员工能够做哪些东西,给出这些分段函数,求最小的愤慨值以满足须要被 ...

  2. UICollectionView使用方法补充(照片轮播墙)

    一 整体功能图和实现思路 1 完整的功能图: 2 实现功思路: 1> 流水布局(实现UICollectionView必需要的条件) 2> 自己定义cell(实现UICollectionVi ...

  3. mac os 虚拟机安装

    https://blog.csdn.net/u011415782/article/details/78505422 感谢

  4. CSS负margin的影响

    原文 简书原文:https://www.jianshu.com/p/87677fd2ea34 相关文章: CSS负边距margin的应用:https://www.cnblogs.com/shcrk/p ...

  5. 【2186】Popular Cows(强连通分支及其缩点)

    id=2186">[2186]Popular Cows(强联通分支及其缩点) Popular Cows Time Limit: 2000MS   Memory Limit: 65536 ...

  6. adb常用命令 分类: H1_ANDROID 2013-09-08 15:22 510人阅读 评论(0) 收藏

    安装软件  adb install apk文件名称.apk  重新安装该软件  adb install -r apk文件名称.apk  卸载apk软件  adb uninstall apk包名.apk ...

  7. Docker容器应用日志查看

    原文:Docker容器应用日志查看 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/benben_2015/article/details/80708 ...

  8. 【10.58%】【codeforces 721C】Journey

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. JavaEE分层知识点粗略解释

    JavaEE知识点总结 什么是分层开发? 一种化大为小,分而治之的软件开发方法. 分层的特点: 1.每一层都有自己的责任. 2.上一层不用关心下一层的实现细节,上一层通过下一层 提供的对外接口来使用其 ...

  10. VMWare提供三种工作模式桥接(bridge)、NAT(网络地址转换)和host-only(主机模式)

    1.桥接模式 在桥接模式下,VMWare虚拟出来的操作系统就像是局域网中的一台独立的主机(主机和虚拟机处于对等地位),它可以访问网内任何一台机器.在桥接模式下,我们往往需要为虚拟主机配置IP地址.子网 ...