B - Tour

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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 解题:据说是双调dp,看不懂,费用流貌似也可以解。
 #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = ;
const int INF = 0x3f3f3f3f;
struct Point {
int x,y;
bool operator<(const Point &t)const {
return x < t.x;
}
} p[maxn];
double ds[maxn][maxn],dp[maxn][maxn];
double calc(int a,int b) {
LL x = p[a].x - p[b].x;
LL y = p[a].y - p[b].y;
double ret = x*x + y*y;
return sqrt(ret);
}
int main() {
int n;
while(~scanf("%d",&n)) {
for(int i = ; i <= n; ++i)
scanf("%d %d",&p[i].x,&p[i].y);
sort(p+,p+n+);
for(int i = ; i <= n; ++i)
for(int j = ; j <= n; ++j)
ds[i][j] = calc(i,j);
dp[][] = ds[][];
for(int i = ; i <= n; ++i) {
dp[i][i-] = INF;
for(int j = ; j < i-; ++j) {
dp[i][j] = dp[i-][j] + ds[i-][i];
dp[i][i-] = min(dp[i][i-],dp[i-][j] + ds[j][i]);
}
}
printf("%.2f\n",dp[n][n-]+ds[n-][n]);
}
return ;
}

xtu read problem training B - Tour的更多相关文章

  1. xtu read problem training 3 B - Gears

    Gears Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 3789 ...

  2. xtu read problem training 3 A - The Child and Homework

    The Child and Homework Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on Code ...

  3. xtu read problem training 2 B - In 7-bit

    In 7-bit Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 3 ...

  4. xtu read problem training 4 A - Moving Tables

    Moving Tables Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ...

  5. xtu read problem training 4 B - Multiplication Puzzle

    Multiplication Puzzle Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. O ...

  6. xtu read problem training A - Dividing

    A - Dividing Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Descri ...

  7. TSP-UK49687

    Copied From:http://www.math.uwaterloo.ca/tsp/uk/index.html Shortest possible tour to nearly every pu ...

  8. A Gentle Guide to Machine Learning

    A Gentle Guide to Machine Learning Machine Learning is a subfield within Artificial Intelligence tha ...

  9. Bias vs. Variance(1)--diagnosing bias vs. variance

    我们的函数是有high bias problem(underfitting problem)还是 high variance problem(overfitting problem),区分它们很得要, ...

随机推荐

  1. Lucas+中国剩余定理 HDOJ 5446 Unknown Treasure

    题目传送门 题意:很裸,就是求C (n, m) % (p1 * p2 * p3 * .... * pk) 分析:首先n,m<= 1e18, 要用到Lucas定理求大组合数取模,当然p[]的乘积& ...

  2. h5-18-文件操作-兼容判断

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. Git命令---递归克隆

    git clone --recursive https://github.com/rbgirshick/fast-rcnn.git Git命令 --recursive 会递归克隆fast-rcnn项目 ...

  4. C51之数据范围

    在C51中各数据类型的范围如下:如果宏常量大于65536,则要加UL后缀:乘法运算不能只将结果强制类型转换,而应在被乘数前加(unsigned long)强制转换. 2 因为RAM有限,所以运算量大的 ...

  5. 百度地图API简单初始化

    <script src="http://api.map.baidu.com/api?key=&v=2.0&ak=youkey"></script& ...

  6. apt-get的一些坑

    apt-get update:更新安装列表apt-get upgrade:升级软件apt-get install software_name :安装软件apt-get --purge remove  ...

  7. [BZOJ1050][HAOI2006]旅行comf 枚举+并查集

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1050 将边排序,枚举边权最小的边,依次加边直到S和T连通,更新答案. #include&l ...

  8. CCF|最大波动|Java|100

    import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Sc ...

  9. element ui select组件和table做分页完整功能和二级联动效果

    <template> <div class="index_box"> <div class="search_box"> &l ...

  10. oracle补丁类型

    名称 说明 Release ¤ 标准产品发布.如Oracle Database 10g Release 2的第一个发行版本为10.2.0.1,可以在OTN.edelivery等站点上公开下载 Patc ...