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. 题解报告:hdu 6440 Dream(费马小定理+构造)

    解题思路:给定素数p,定义p内封闭的加法和乘法运算(运算封闭的定义:若从某个非空数集中任选两个元素(同一元素可重复选出),选出的这两个元素通过某种(或几种)运算后的得数仍是该数集中的元素,那么,就说该 ...

  2. debug授权码

    www.vfxcx.com 704835b5c54b56426257e0742568fe54

  3. G. 24 观察 + 树状数组

    http://codeforces.com/gym/101257/problem/G 首先要看到题目,题目是本来严格大于score[i] > score[j].然后score[i] < s ...

  4. D. Tavas and Malekas DFS模拟 + kmp + hash || kmp + hash

    http://codeforces.com/contest/535/problem/D 如果真的要把m个串覆盖上一个串上面,是可以得,不会超时. 要注意到一点,全部覆盖后再判断时候合法,和边放边判断, ...

  5. 使用VMwaver 克隆CentOS 6.9网卡配置报错

    报错信息: 克隆完成之后,使用的是NAT模式,进入系统之后有IP地址也可以ping外网,但是没有ifcfg-eth0这个文件,使用setup命令配置网卡时报以下错误: 待解决-

  6. Xml文档数据提取到Excel表中

    近期,财务一位同事,吐槽:<某XX开票软件>导出数据文档只有Xml格式,竟然没有Excel文档,工作起来非常不方便,希望我想想办法.上图: 需求分析:Xml数据----> 提取到Da ...

  7. windows 下防火墙安全加固,配置规则

    netsh advfirewall firewall: 显示关于防火墙操作的常见命令的帮助信息 netsh advfirewall firewall show rule name=all dir=in ...

  8. 获取声音设备名称及PNPDeviceID

    实现效果: 知识运用: WMI管理类中的 ManagementObjectSearcher类 //基于指定的查询检索管理对象的集合 用于检索管理信息的较为常用的入口点之一 public Managem ...

  9. redis.conf介绍

    默认配置文件: # Redis configuration file example. # # Note that in order to read the configuration file, R ...

  10. Java数据结构和算法(一)--栈

    栈: 英文名stack,特点是只允许访问最后插入的那个元素,也就是LIFO(后进先出) jdk中的stack源码: public class Stack<E> extends Vector ...