UVa OJ 1347 - Tour
来源:互联网 发布:mac找不到硬盘 编辑:程序博客网 时间:2024/06/11 18:25
UVa OJ 1347 - Tour
Problem
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 12 33 14 1 1 2 33 14 2
Sample Output
6.477.89
Solution
又是一道动态规划的题目
len[i][j] 代表终点到x[i],x[j]的距离之和
‘std::setprecision()’ 能帮助我们控制浮点数的有效数字. 而加上 ‘std::cout.setf(ios::fixed)’ 则能让我们设置小数点后保留的位数
#include <iostream>#include <cstdio>#include <iomanip>#include <cmath>using namespace std;const int maxn = 61;int n;double x[maxn], y[maxn];double dist[maxn][maxn], len[maxn][maxn];int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); //freopen("input.txt" , "r", stdin ); //freopen("output.txt", "w", stdout); while(cin >> n) { for (int i = 0; i < n; ++i) cin >> x[i] >> y[i]; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) dist[i][j] = sqrt((x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j])); for (int i = n-2; i > 0; --i) { for (int j = 0; j < i; ++j) { if ( i == n-2) len[i][j] = dist[i][n-1] + dist[j][n-1]; else len[i][j] = min(len[i+1][j]+dist[i+1][i], len[i+1][i]+dist[i+1][j]); } } double ans = dist[1][0] + len[1][0]; cout.setf(ios::fixed); cout << setprecision(2) << ans << '\n'; } return 0;}
- UVa OJ 1347 - Tour
- uva 1347 - Tour
- Tour - UVa 1347 dp
- UVa 1347 Tour(DP)
- UVa 1347 Tour(DP)
- UVA 1347 Tour DP
- UVa - 1347 - Tour
- UVA - 1347 Tour
- uva 1347 tour
- uva 1347 tour
- uva 1347 Tour 旅行
- UVA 1347 Tour
- UVa 1347 Tour
- uva 1347 Tour
- UVa 1347 Tour
- UVA 1347 Tour
- uva 1347 tour
- Tour UVa 1347
- 事务的简单介绍
- Golang Http Server源码阅读
- android获取系统时间24小时制
- studio 引用aar 声明
- (转自微博) 分布式缓存架构基础
- UVa OJ 1347 - Tour
- mysql 运行 net start mysql 报“服务名无效”解决办法
- Git入门
- 无
- poi批量下载功能
- 关于android的设计模式---MVP的个人理解
- XML(Extensible Markup Language)基础
- Android Studio Gradle更新
- 6-超链接