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. Mysql游标

    14.6.6.1 Cursor CLOSE Syntax 14.6.6.2 Cursor DECLARE Syntax 14.6.6.3 Cursor FETCH Syntax 14.6.6.4 Cu ...

  2. OkHttp学习总结

    This paper mainly includes the following contents okhttp ordinary operation. okhttp interceptors. Re ...

  3. java向oracle数据库中插入当前时间

    public class Test{public static void main (String args []){ java.util.Date a = new java.util.Date(); ...

  4. 二、JavaScript语言--JS基础--JavaScript进阶篇--JavaScript内置对象

    1.什么事对象 JavaScript 中的所有事物都是对象,如:字符串.数值.数组.函数等,每个对象带有属性和方法. 对象的属性:反映该对象某些特定的性质的,如:字符串的长度.图像的长宽等: 对象的方 ...

  5. DNS原理

    DNS 是互联网核心协议之一.不管是上网浏览,还是编程开发,都需要了解一点它的知识. 本文详细介绍DNS的原理,以及如何运用工具软件观察它的运作.我的目标是,读完此文后,你就能完全理解DNS. 一.D ...

  6. Bitmap在Java中的实现和应用

    >>40亿数据排序问题 给定一个最多包含40亿个随机排列的32位整数的顺序文件,找出一个不在文件中的32位整数(在文件中至少缺失这样一个数——为什么?).在具有足够内存的情况下,如何解决该 ...

  7. [LeetCode] Word Pattern

    Word Pattern Total Accepted: 4627 Total Submissions: 17361 Difficulty: Easy Given a pattern and a st ...

  8. mac下php开发环境搭建+CI框架使用

    一.启动apache: apachectl start 停止: apachectl stop 配置文件: vi /etc/apache2/httpd.conf 一.修改端口 因为80端口不想被占用,8 ...

  9. Oracle【IT实验室】数据库备份与恢复之二:SQL*Loader

    2.1 基本知识 Oracle 的  SQL* LOADER  可以将外部格式化的文本数据加载到数据库表中.通常 与 SPOOL导出文本数据方法配合使用.     1.命令格式 SQLLDR keyw ...

  10. PHP日期操作类代码-农历-阳历转换、闰年、计算天数等

    <?php class Lunar { var $MIN_YEAR = 1891; var $MAX_YEAR = 2100; var $lunarInfo = array( array(0,2 ...