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

Time Limit: 3 Sec  Memory Limit: 64 MB
Submit: 464  Solved: 331
[Submit][Status][Discuss]

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

HINT

 

Source

凸包 卡壳

Solution

Graham扫描法求凸包,模板题

(1)找出点集p[]中最左下的点p1,把p1同点集中其他各点用线段连接,并计算这些线段与水平线的夹角,然后按夹角从小到大和按到p1的距离从近到远排序(夹角范围为 [0, 180)度,而且可以删除相同夹角且距离p1较近的点,保留最远点,这样可减少计算量。因为直线上的非端点不是凸包的极点,即如果p1,p2,p3在一条直线上,则只取凸点p1,p3。p2不在端点,故可以去掉),得到新的节点序列p1,p2,...pn.依次连接这些点,得到一个多边形(已经逆时针,有所进展,但还需去掉不在凸包上的点)。此时p1是凸包的边界起点,p2和pn也是最终凸包的顶点,p[n+1]=p1(看成循环的)

(2)删除p3,p4,...p[n-1]中不在凸包上的点:

先把p1,p2,p3入栈S中,再依次循环(i = 3 -> n-1),若栈顶的两个点和当前的点p[i]这三点连线的方向向顺时针方向偏转,表明是凹的,应删除,则栈顶元素出栈(要循环判断,即可能前面的仍是凹的,还需再出栈,举例如下图),直到向逆时针方向偏转或者栈内只有2个元素了(p1p2),就把当前点p[i]入栈。

最后栈中的元素就是最终凸包上的点。

Code

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
using namespace std;
struct Vector
{
double x,y;
Vector (double X=,double Y=) {x=X; y=Y;}
};
typedef Vector Point;
#define MAXN 5010
Point P[MAXN],ch[MAXN];
int n;
#define eps 1e-8
int dcmp(double x) {if (fabs(x)<eps) return ; return x<? -:;}
Vector operator + (Vector A,Vector B) {return ((Vector){A.x+B.x,A.y+B.y});}
Vector operator - (Vector A,Vector B) {return ((Vector){A.x-B.x,A.y-B.y});}
Vector operator * (Vector A,double p) {return ((Vector){A.x*p,A.y*p});}
Vector operator / (Vector A,double p) {return ((Vector){A.x/p,A.y/p});}
double Dot(Vector A,Vector B) {return A.x*B.x+A.y*B.y;}
double Cross(Vector A,Vector B) {return A.x*B.y-A.y*B.x;}
double dis(Point A,Point B) {return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));}
bool operator < (const Vector& a,const Vector& b) {return a.x<b.x||(a.x==b.x&&a.y<b.y);}
int Graham_ConvexHull(Point *p,int num,Point *ch)
{
sort(p,p+n);
int m=;
for (int i=; i<num; i++)
{
while (m> && dcmp(Cross(ch[m-]-ch[m-],p[i]-ch[m-]))<=) m--;
ch[m++]=p[i];
}
int k=m;
for (int i=n-; i>=; i--)
{
while (m>k && dcmp(Cross(ch[m-]-ch[m-],p[i]-ch[m-]))<=) m--;
ch[m++]=p[i];
}
if (num>) m--;
return m;
}
double ans=;
int main()
{
scanf("%d",&n);
for (int i=; i<=n-; i++)
scanf("%lf%lf",&P[i].x,&P[i].y);
int m=Graham_ConvexHull(P,n,ch);
ch[m+]=ch[];
for (int i=; i<=m; i++) ans+=dis(ch[i],ch[i+]);
printf("%.2lf\n",ans);
return ;
}

模板题差点没1A...吓死我了

【BZOJ-1670】Building the Moat护城河的挖掘 Graham扫描法 + 凸包的更多相关文章

  1. bzoj 1670 Building the Moat护城河的挖掘 —— 凸包

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1670 单调栈维护凸包即可,用叉积判断: 维护上凸壳,然后把所有点的纵坐标翻转再求上凸壳即可, ...

  2. BZOJ_1670_[Usaco2006 Oct]Building the Moat护城河的挖掘_求凸包

    BZOJ_1670_[Usaco2006 Oct]Building the Moat护城河的挖掘_求凸包 Description 为了防止口渴的食蚁兽进入他的农场,Farmer John决定在他的农场 ...

  3. bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 -- 凸包

    1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 Time Limit: 3 Sec  Memory Limit: 64 MB Description 为了防止 ...

  4. bzoj1670【Usaco2006 Oct】Building the Moat 护城河的挖掘

    1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 Time Limit: 3 Sec  Memory Limit: 64 MB Submit: 387  Sol ...

  5. 【BZOJ】1670: [Usaco2006 Oct]Building the Moat护城河的挖掘(凸包)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1670 裸打了凸包.. #include <cstdio> #include <cs ...

  6. 牛客假日团队赛5J 护城河 bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 (凸包的周长)

    链接:https://ac.nowcoder.com/acm/contest/984/J 来源:牛客网 护城河 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...

  7. BZOJ 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘

    Description 求凸包周长. Sol 凸包+计算几何. 这好像叫什么 Graham Scan 算法... 这个可以求凸包的周长,直径,面积. 选择一个基点,然后按极角排序,最后用一个栈一直维护 ...

  8. bzoj 1670 [Usaco2006 Oct]Building the Moat护城河的挖掘——凸包

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1670 用叉积判断.注意两端的平行于 y 轴的. #include<cstdio> ...

  9. bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘【凸包】

    凸包模板 #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> ...

随机推荐

  1. PHP程序设计

    ① 在设计评论系统时,关于文章的一些属性,我们最好存放在一个内存缓存中,通过下面的设计每次仅需要查询一次即可获取文章的所有属性 class CommentsDoc{ private static $d ...

  2. ES6 数组解构赋值

    .数组解构 let [a, b, c,d] = ["aa", "bb", 77,88]; alert(a) //弹出aa 可以用babel 解析看ES5的转换结 ...

  3. 数据表格 - DataGrid - 列表显示

    <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding=& ...

  4. hadoop: hbase1.0.1.1 伪分布安装

    环境:hadoop 2.6.0 + hbase 1.0.1.1 + mac OS X yosemite 10.10.3 安装步骤: 一.下载解压 到官网 http://hbase.apache.org ...

  5. vtk renderer / rendering 绘制

    1.在绘制窗口中绘制出物体(静态的)vtkRenderWindow * w=vtkRenderWindow::New();  w->AddRenderer(r);        for(int ...

  6. python数字图像处理(18):高级形态学处理

    形态学处理,除了最基本的膨胀.腐蚀.开/闭运算.黑/白帽处理外,还有一些更高级的运用,如凸包,连通区域标记,删除小块区域等. 1.凸包 凸包是指一个凸多边形,这个凸多边形将图片中所有的白色像素点都包含 ...

  7. Mininet的内部实现原理简介

    原文发表在我的博客主页,转载请注明出处. 前言 之前模拟仿真网络一直用的是Mininet,包括写了一些关于Mininet安装,和真实网络相连接,Mininet简历拓扑的博客,但是大多数都是局限于具体步 ...

  8. GridView的 OnRowDataBound 事件用法

    <asp:GridView ID="RptUsers" runat="server" AutoGenerateColumns="False&qu ...

  9. sql server存储过程编程

    存储过程是一组完成特定功能的SQL 语句集合,经编译后存储在数据库中.   存储过程作为一个单元进行处理并以一个名称来标识.它能向用户返回数据.向数据库表中写入或修改数据等操作. 用户通过指定存储过程 ...

  10. ModernUI教程:目录 (完结)

    入门 My first Modern UI app (manually)                         第一个ModernUI应用(手动编写)(已完成) My first Moder ...