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 ...
随机推荐
- 2. Python中的基本输入、输出、格式化
本文利用的是Python 3.x版本,建议学习3.x版本 Python中的基本输入.输出.格式化 1. 输入 使用input([prompt])读取一行,将其转换为string类型并返回,input的 ...
- 【5】Django项目配置settings.py详解
夫唯不争,故天下莫能与之争 --老子<道德经> 本节内容 1.项目配置文件settings.py介绍 2.数据库配置[MySQL] 3.创建模型对象并和数据库同步 4.python官方提供 ...
- PAT 1114 Family Property
This time, you are supposed to help us collect the data for family-owned property. Given each person ...
- Navicat premium连接Oracle报ORA-12545错误
1:ORA-12545 原因: 这里填localhost,127.0.0.1,或者远程ip.
- RSAROLL
题目:http://www.shiyanbar.com/ctf/1918 # -*- coding: utf-8 -*- import gmpy2 ciper = [704796792, 752211 ...
- 使用git bash向github远程仓库提交代码
1.登录github,创建仓库. 2.切换到要提交的文件目录下. 3.打开git bash 3.1.初始化仓库 git init 3.2.将本地仓库与远程仓库关联 git remote add ori ...
- G - Power Strings
Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc&quo ...
- [bzoj2802][Poi2012]Warehouse Store_贪心_堆
Warehouse Store bzoj-2802 Poi-2012 题目大意:一家商店的连续n天内,每一天会进货$a_i$个,有且只有一个客人回来买$b_i$个,问至多满足多少人. 注释:$1\le ...
- 洛谷——P2925 [USACO08DEC]干草出售Hay For Sale
https://www.luogu.org/problem/show?pid=2925 题目描述 Farmer John suffered a terrible loss when giant Aus ...
- not in 和 <> 不走索引
首先我们要知道的一点就是CBO的代码oracle是不会对我们公开的,起码现在是.所以本文中的结论不一定适用所有的版本.在应用本文的结论之前最好先试一下. ok 下面就是本文的结论,当你在where语句 ...