1143. Electric Path

Time limit: 1.0 second
Memory limit: 64 MB

Background

At the team competition of the 10th national student informatics Olympic, which is organized at Hanoi National University, there are N teams participating. Each team is assigned to work in a camp. On the map, it can be seen that the camps are positioned on the vertices of a convex polygon with Nvertices: P1, P2, …, PN (the vertices are enumerated around the polygon in counter-clockwise order.) In order to achieve absolute safety providing electricity to the camps, besides an electric supplying system, the host organization set up a path from a reserved electricity generator (which is placed in one of the camps) to every camp once, and the path's total length is minimum.

Problem

Given the coordinates of the polygons' vertices (the camps' positions), determine the length of the electric path corresponding to the host organization's arrangement.

Input

The first line contains the integer N (1 ≤ N ≤ 200). The i'th line of the next N lines contains two real numbers xiyi, separated by a space, with no more than 3 digits after the decimal points, are vertex Pi's coordinates on the plane (with i = 1, 2, …, N). The length of the path connecting two vertex (xiyi) and (xjyj) is computed with the formula: sqrt((xi − xj)2 + (yi − yj)2).

Output

The only line should contain real number L (written in real number format, with 3 digits after the decimal point), which is the total length of the electric path.

Sample

input output
4
50.0 1.0
5.0 1.0
0.0 0.0
45.0 0.0
50.211
Problem Source: The competition for selecting the Vietnam IOI team
Difficulty: 532 
 
题意:顺序给出平面上凸多边形上n个点,问用一条线连接它们的最短长度。
分析:首先,可以感觉出线是不会交叉的。
然后我们证明一下:
  先考虑四个点的情况。
  取交叉的四个点。如果交叉,那么这四个点组成的四边形,肯定两条对角线都会被选择,然后再加上某一条边。
  那么,显然,可以将一条对角边改为一条相邻的边(连接相邻的点)。这样显然更短。
  考虑更多点的情况。
  都可以选择两条相交的线的两个端点,共四个点。
  然后将其他点抽象成边,由于是凸多边形,同样的性质必定满足。
  故可以抽象为四个点的情况。
 
然后有这个性质就可以做了。
从两边开始dp,dp[i][j][0..1]表示从第i个到第j个,从左边还是右边开始连线。
转移显然了,只有两种。
 /**
Create By yzx - stupidboy
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define ft first
#define sd second
#define mk make_pair inline int Getint()
{
int Ret = ;
char Ch = ' ';
bool Flag = ;
while(!(Ch >= '' && Ch <= ''))
{
if(Ch == '-') Flag ^= ;
Ch = getchar();
}
while(Ch >= '' && Ch <= '')
{
Ret = Ret * + Ch - '';
Ch = getchar();
}
return Flag ? -Ret : Ret;
} const int N = ;
struct Point
{
DB x, y; inline void Read()
{
scanf("%lf%lf", &x, &y);
}
} arr[N];
int n;
DB dp[N][N][];
bool visit[N][N][]; inline void Input()
{
scanf("%d", &n);
for(int i = ; i <= n; i++) arr[i].Read();
} inline DB Sqr(DB x)
{
return x * x;
} inline DB Dist(const Point &A, const Point &B)
{
return sqrt(Sqr(A.x - B.x) + Sqr(A.y - B.y));
} inline DB Work(int left, int right, bool type)
{
if(left >= right) return ;
if(visit[left][right][type])
return dp[left][right][type];
visit[left][right][type] = ;
DB ret = 1.0 * INF, cnt;
if(type == )
{
cnt = Dist(arr[left], arr[left + ]);
cnt += Work(left + , right, );
ret = min(ret, cnt); cnt = Dist(arr[left], arr[right]);
cnt += Work(left + , right, );
ret = min(ret, cnt);
}
else
{
cnt = Dist(arr[right], arr[right - ]);
cnt += Work(left, right - , );
ret = min(ret, cnt); cnt = Dist(arr[right], arr[left]);
cnt += Work(left, right - , );
ret = min(ret, cnt);
} return dp[left][right][type] = ret;
} inline void Solve()
{
for(int i = n + ; i <= * n; i++)
arr[i] = arr[i - n]; DB ans = 1.0 * INF, cnt;
for(int i = ; i <= n; i++)
{
cnt = Work(i, i + n - , );
ans = min(ans, cnt);
cnt = Work(i, i + n - , );
ans = min(ans, cnt);
} printf("%.3lf\n", ans);
} int main()
{
freopen("a.in", "r", stdin);
Input();
Solve();
return ;
}

ural 1143. Electric Path的更多相关文章

  1. ural 1143. Electric Path(凸包上最短哈密顿路径)

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1143 题意:逆时针给一个凸包的n(n<=200)个顶点坐标,求一个最短哈密顿路径的 ...

  2. dp题目列表

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

  3. poj 动态规划题目列表及总结

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

  4. poj动态规划列表

    [1]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 13 ...

  5. POJ 动态规划题目列表

    ]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 1322 ...

  6. poj 动态规划的主题列表和总结

    此文转载别人,希望自己可以做完这些题目. 1.POJ动态规划题目列表 easy:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, ...

  7. 别人整理的dp题目

    动态规划 动态规划 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 1322, 14 ...

  8. 别人整理的DP大全(转)

    动态规划 动态规划 容易: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ...

  9. URAL 1525 Path

    #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> us ...

随机推荐

  1. 搞笑世界杯(codevs 1060)

    题目描述 Description 随着世界杯小组赛的结束,法国,阿根廷等世界强队都纷纷被淘汰,让人心痛不已. 于是有 人组织了一场搞笑世界杯,将这些被淘汰的强队重新组织起来和世界杯一同比赛.你和你的朋 ...

  2. 自己动手编译hadoop-2.5.2源码

    搭建环境:Centos x 6.5  64bit (后来:我才知道原来官网上发布的就是64位的,不过这个对我来说是个学习过程,对以后进行其他平台编译的时候有帮助!) 1.安装JDK 我这里用的是64位 ...

  3. SQLSERVER查询连接数

    SELECT * FROM [Master].[dbo].[SYSPROCESSES] WHERE [DBID] IN (SELECT [DBID]FROM [Master].[dbo].[SYSDA ...

  4. TCP 四次握手

    TCP协议中的三次握手和四次挥手(图解) http://blog.csdn.net/whuslei/article/details/6667471/

  5. HTML5学习之视频与音频(三)

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  6. Linux 下 Console / 控制台 复制粘贴快捷键

    Linux下复制粘贴快捷键 1. 在终端下: 复制命令:Ctrl + Shift + C  组合键. 粘贴命令:Ctrl + Shift + V  组合键. 2. 在控制台下: 复制命令:Ctrl + ...

  7. Delphi面向对象编程

    一.面向对象介绍 OOP是使用独立的对象(包含数据和代码)作为应用程序模块的范例.虽然OOP不能使得代码容易编写,但是它能够使得代码易于维护.将数据和代码结合在一起,能够使定位和修复错误的工作简单化, ...

  8. MVC中Action 过滤

    总结Action过滤器实用功能,常用的分为以下两个方面: 1.Action过滤器主要功能就是针对客服端请求过来的对象行为进行过滤,类似于门卫或者保安的职能,通过Action过滤能够避免一些非必要的深层 ...

  9. 攻城狮在路上(壹) Hibernate(九)--- Hibernate的映射类型

    Hibernate采用映射类型作为Java类型和SQL类型的桥梁,对应type属性.分为两种:内置映射类型和客户化映射类型.一.内置映射类型: 1.Java基本类型的Hibernate映射类型: Ja ...

  10. android 入门-android属性介绍

      android:visibility="gone" 不保留view控件所占有的空间 隐藏 android:visibility="invisible" 保留 ...