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. JDBC——入门知识【转】

      1. 什么是JDBC:Java数据库连接性(JavaDatabase Connectivity) API,允许用户从Java应用程序中访问任何表格化数据源. 2. JDBC除了提供到更宽范围的SQ ...

  2. 生成HTML表格的后台模板代码

    有时候,我们需要在后台拼接生成前端的html表格,一般的做法就是各种string.StringBuilder的拼接(例子省略...),这样的话如果表头不同就没法做到代码的重用,增加代码的冗余,下面我分 ...

  3. ASP.NET MVC5的一个轻量级的框架学习的第一天

    第二步第三部 这是第一天的小试成功,怪自己太笨了,一个错排查好久,还好有源码看着了解,后续还得多努力,

  4. 响应式布局 max-device-width 与 max-width 的区别

    闲来没事,研究了一下多屏适配和响应式布局的 CSS. 第一种写法 @media screen and (max-device-width: 320px) { } @media screen and ( ...

  5. C# 调用第三方DLL缓冲区溢出导致的异常

    这个倒是少见的错误,纪录一下大佬. 先上异常 错误一:尝试读取或写入受保护的内存 错误二:未将对象引用设置到对象的实例 错误三:  托管调试助手“FatalExecutionEngineError”( ...

  6. (转)使用CGLIB实现AOP功能与AOP概念解释

    http://blog.csdn.net/yerenyuan_pku/article/details/52864395 使用CGLIB实现AOP功能 在Java里面,我们要产生某个对象的代理对象,这个 ...

  7. mysql查询-从表1中查询出来的结果重新插入到表1

    原有表结构 CREATE TABLE `t_card_user` ( `id` varchar(32) NOT NULL, `card_user_id` bigint(20) DEFAULT NULL ...

  8. MFC中EDIT控件实现换行

    \n是C下的回撤换行.在MFC下得用\r\n.

  9. 【软件构造】第三章第五节 ADT和OOP中的等价性

    第三章第五节 ADT和OOP中的等价性 在很多场景下,需要判定两个对象是否 “相等”,例如:判断某个Collection 中是否包含特定元素. ==和equals()有和区别?如何为自定义 ADT正确 ...

  10. Linux-02 Linux常用命令

    学习要点 用户切换 网络设置 目录操作 挂载 文件操作 用户切换 登陆时候选择其他用户为root则默认密码和系统默认用户一致 例如设置用户为centos1,密码为centos1,则root用户的密码同 ...