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
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 ;
}
上一篇:jade的写法


下一篇:git 常见命令解析