bzoj1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 计算几何 凸包

bzoj1670: [Usaco2006 Oct]Building the Moat护城河的挖掘

Description

为了防止口渴的食蚁兽进入他的农场,Farmer John决定在他的农场周围挖一条护城河。农场里一共有N(8<=N<=5,000)股泉水,并且,护城河总是笔直地连接在河道上的相邻的两股泉水。护城河必须能保护所有的泉水,也就是说,能包围所有的泉水。泉水一定在护城河的内部,或者恰好在河道上。当然,护城河构成一个封闭的环。 挖护城河是一项昂贵的工程,于是,节约的FJ希望护城河的总长度尽量小。请你写个程序计算一下,在满足需求的条件下,护城河的总长最小是多少。 所有泉水的坐标都在范围为(1..10,000,000,1..10,000,000)的整点上,一股泉水对应着一个唯一确定的坐标。并且,任意三股泉水都不在一条直线上。 以下是一幅包含20股泉水的地图,泉水用”*”表示

图中的直线,为护城河的最优挖掘方案,即能围住所有泉水的最短路线。 路线从左上角起,经过泉水的坐标依次是:(18,0),(6,-6),(0,-5),(-3,-3),(-17,0),(-7,7),(0,4),(3,3)。绕行一周的路径总长为70.8700576850888(…)。答案只需要保留两位小数,于是输出是70.87。

Input
  • 第1行: 一个整数,N * 第2..N+1行: 每行包含2个用空格隔开的整数,x[i]和y[i],即第i股泉水的位 置坐标
Output
  • 第1行: 输出一个数字,表示满足条件的护城河的最短长度。保留两位小数
Sample Input

20
2 10
3 7
22 15
12 11
20 3
28 9
1 12
9 3
14 14
25 6
8 1
25 1
28 4
24 12
4 15
13 5
26 5
21 11
24 4
1 8

Sample Output

70.87

代码

裸凸包

/**************************************************************
    Problem: 1670
    User: 2014lvzelong
    Language: C++
    Result: Accepted
    Time:28 ms
    Memory:1388 kb
****************************************************************/

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<cmath>
using namespace std;
const int N = 5500;
int read() {
    char ch = getchar(); int x = 0, f = 1;
    while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
    while(ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int n, top;
struct point {
    int x, y;
    point(int a = 0, int b = 0) : x(a), y(b) {}
    long long operator * (point a) {return (long long)x * a.y - y * a.x;}
    point operator - (point a) {return point(x - a.x, y - a.y);}
    bool cmp(point a) {return y == a.y ? x < a.x : y < a.y;}
}p[N], st[N];
long long sqr(int x) {return (long long)x * x;}
long long dis(point a, point b) {return sqr(a.x - b.x) + sqr(a.y - b.y);}
long long xmult(point a, point b, point c) {return (b - a) * (c - a);}
bool operator < (point a, point b) {
    long long tmp = xmult(p[1], a, b);
    return tmp == 0 ? dis(p[1], a) < dis(p[1], b) : tmp > 0;
}

void Graham() {
    int k = 1;
    for(int i = 2;i <= n; ++i)
        if(p[i].cmp(p[k])) k = i;
    swap(p[1], p[k]);
    sort(p + 2, p + n + 1);
    st[++top] = p[1]; st[++top] = p[2];
    for(int i = 3;i <= n; ++i) {
        while(xmult(st[top - 1], st[top], p[i]) <= 0) --top;
        st[++top] = p[i];
    }
    st[top + 1] = p[1]; 
}

int main() { 
    n = read();
    for(int i = 1;i <= n; ++i) p[i].x = read(), p[i].y = read();
    Graham(); double ans = 0;
    for(int i = 1;i <= top; ++i) ans += sqrt(dis(st[i], st[i + 1]));
    printf("%.2lf", ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值