ural 1143. Electric Path

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

Tags: none  (hide tags for unsolved problems)

Difficulty: 532

题意:顺序给出平面上凸多边形上n个点,问用一条线连接它们的最短长度。

分析:首先,可以感觉出线是不会交叉的。

然后我们证明一下:

  先考虑四个点的情况。

  取交叉的四个点。如果交叉,那么这四个点组成的四边形,肯定两条对角线都会被选择,然后再加上某一条边。

  那么,显然,可以将一条对角边改为一条相邻的边(连接相邻的点)。这样显然更短。

  考虑更多点的情况。

  都可以选择两条相交的线的两个端点,共四个点。

  然后将其他点抽象成边,由于是凸多边形,同样的性质必定满足。

  故可以抽象为四个点的情况。

然后有这个性质就可以做了。

从两边开始dp,dp[i][j][0..1]表示从第i个到第j个,从左边还是右边开始连线。

转移显然了,只有两种。

  1 /**
  2 Create By yzx - stupidboy
  3 */
  4 #include <cstdio>
  5 #include <cstring>
  6 #include <cstdlib>
  7 #include <cmath>
  8 #include <deque>
  9 #include <vector>
 10 #include <queue>
 11 #include <iostream>
 12 #include <algorithm>
 13 #include <map>
 14 #include <set>
 15 #include <ctime>
 16 #include <iomanip>
 17 using namespace std;
 18 typedef long long LL;
 19 typedef double DB;
 20 #define MIT (2147483647)
 21 #define INF (1000000001)
 22 #define MLL (1000000000000000001LL)
 23 #define sz(x) ((int) (x).size())
 24 #define clr(x, y) memset(x, y, sizeof(x))
 25 #define puf push_front
 26 #define pub push_back
 27 #define pof pop_front
 28 #define pob pop_back
 29 #define ft first
 30 #define sd second
 31 #define mk make_pair
 32
 33 inline int Getint()
 34 {
 35     int Ret = 0;
 36     char Ch = ‘ ‘;
 37     bool Flag = 0;
 38     while(!(Ch >= ‘0‘ && Ch <= ‘9‘))
 39     {
 40         if(Ch == ‘-‘) Flag ^= 1;
 41         Ch = getchar();
 42     }
 43     while(Ch >= ‘0‘ && Ch <= ‘9‘)
 44     {
 45         Ret = Ret * 10 + Ch - ‘0‘;
 46         Ch = getchar();
 47     }
 48     return Flag ? -Ret : Ret;
 49 }
 50
 51 const int N = 410;
 52 struct Point
 53 {
 54     DB x, y;
 55
 56     inline void Read()
 57     {
 58         scanf("%lf%lf", &x, &y);
 59     }
 60 } arr[N];
 61 int n;
 62 DB dp[N][N][2];
 63 bool visit[N][N][2];
 64
 65 inline void Input()
 66 {
 67     scanf("%d", &n);
 68     for(int i = 1; i <= n; i++) arr[i].Read();
 69 }
 70
 71 inline DB Sqr(DB x)
 72 {
 73     return x * x;
 74 }
 75
 76 inline DB Dist(const Point &A, const Point &B)
 77 {
 78     return sqrt(Sqr(A.x - B.x) + Sqr(A.y - B.y));
 79 }
 80
 81 inline DB Work(int left, int right, bool type)
 82 {
 83     if(left >= right) return 0;
 84     if(visit[left][right][type])
 85         return dp[left][right][type];
 86     visit[left][right][type] = 1;
 87     DB ret = 1.0 * INF, cnt;
 88     if(type == 0)
 89     {
 90         cnt = Dist(arr[left], arr[left + 1]);
 91         cnt += Work(left + 1, right, 0);
 92         ret = min(ret, cnt);
 93
 94         cnt = Dist(arr[left], arr[right]);
 95         cnt += Work(left + 1, right, 1);
 96         ret = min(ret, cnt);
 97     }
 98     else
 99     {
100         cnt = Dist(arr[right], arr[right - 1]);
101         cnt += Work(left, right - 1, 1);
102         ret = min(ret, cnt);
103
104         cnt = Dist(arr[right], arr[left]);
105         cnt += Work(left, right - 1, 0);
106         ret = min(ret, cnt);
107     }
108
109     return dp[left][right][type] = ret;
110 }
111
112 inline void Solve()
113 {
114     for(int i = n + 1; i <= 2 * n; i++)
115         arr[i] = arr[i - n];
116
117     DB ans = 1.0 * INF, cnt;
118     for(int i = 1; i <= n; i++)
119     {
120         cnt = Work(i, i + n - 1, 0);
121         ans = min(ans, cnt);
122         cnt = Work(i, i + n - 1, 1);
123         ans = min(ans, cnt);
124     }
125
126     printf("%.3lf\n", ans);
127 }
128
129 int main()
130 {
131     freopen("a.in", "r", stdin);
132     Input();
133     Solve();
134     return 0;
135 }

时间: 2024-12-28 13:40:16

ural 1143. Electric Path的相关文章

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

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1143 题意:逆时针给一个凸包的n(n<=200)个顶点坐标,求一个最短哈密顿路径的长度. 解法:求最短哈密顿本身是一个NP问题,但是因为是凸包上,可以利用这个做:有一个性质:凸包上的最短哈密顿路径不会出现交叉.所以可以看出来从一点出发,他要么和顺时针相邻点连接,要么和逆时针相邻点相连接:通过这个性质可以通过dp做: ans[i][j][0]表示i开始,往后j的点最短路径长度,ans[i][

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

此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276,1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740(博弈),1742, 1887, 1926(马尔科夫矩阵,求平衡), 1936, 1952, 1953, 1958, 1959, 1962, 1975,

URAL 1525 Path

#include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> using namespace std; const int maxn = 100000 + 10; char s[maxn]; int main() { long long int n, m, k; while (~scanf("%lld%lld%lld", &n, &m,

URAL 2005. Taxi for Programmers (最短路 数学啊)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2005 2005. Taxi for Programmers Time limit: 0.5 second Memory limit: 64 MB The clock shows 11:30 PM. The sports programmers of the institute of maths and computer science have just finished their trai

ural 2030

Awesome Backup System Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice URAL 2030 Description It is known that all people can be divided into two groups: those who have never lost important data and t

URAL 1932 The Secret of Identifier 题解

http://acm.timus.ru/problem.aspx?space=1&num=1932 B - The Secret of Identifier Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice URAL 1932 Description Davy Jones: You've been captain of the Black Pearl for

URAL 1934 spfa算法

D - Black Spot Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice URAL 1934 Description Bootstrap: Jones's terrible leviathan will find you and drag the Pearl back to the depths and you along with it.

Linux下修改环境变量PATH

1.什么是环境变量(PATH) 在Linux中,在执行命令时,系统会按照PATH的设置,去每个PATH定义的路径下搜索执行文件,先搜索到的文件先执行. 我们知道查阅文件属性的指令ls 完整文件名为:/bin/ls(这是绝对路径), 那你会不会觉得很奇怪:"为什么我可以在任何地方执行/bin/ls这个指令呢? " 为什么我在任何目录下输入 ls 就一定可以显示出一些讯息而不会说找不到该 /bin/ls 指令呢? 这是因为环境变量 PATH 的帮助所致呀! 当我们在执行一个指令癿时候,举例

Description Resource Path Location Type The superclass &quot;javax.servlet.http.HttpServlet&quot; was not foun

一段时间没亲自建新项目玩乐,今天建立了一Maven project的时候发现了以下异常,Description Resource Path Location Type The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path index.jsp /easyBuy/src/main/webapp line 1 JSP Problem 经过查找原因,原来是因为忘记设置server