bzoj1670 Usaco2006 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

 1 /**/
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cmath>
 5 #include<cstring>
 6 #include<algorithm>
 7 #define ll long long
 8 using namespace std;
 9 const int mxn=10000;
10 int n;
11 struct P{
12     int x,y;
13 }p[mxn],s[mxn];
14 int top=0;
15 double sum=0;
16
17 inline int read(){
18     int x=0,f=1;
19     char ch=getchar();
20     while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();};
21     while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();};
22     return x*f;
23 }
24 inline P operator -(P a,P b){
25     P t;    t.x=a.x-b.x;    t.y=a.y-b.y;    return t;
26 }
27 inline ll operator *(P a,P b){
28     return a.x*b.y-a.y*b.x;
29 }
30 inline ll dis(P a,P b){
31     return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
32 }
33 inline bool operator < (P a,P b){
34     ll t=(a-p[1])*(b-p[1]);
35     if(t==0)return dis(p[1],a)<dis(p[1],b);
36     return t>0;
37 }
38
39 void graham(){
40     int t=1,i;
41     for(int i=2;i<=n;i++)
42         if(p[i].y<p[t].y || (p[i].y==p[t].y&&p[i].x<p[t].x))t=i;//找出y值最小点作为起点
43     swap(p[1],p[t]);
44     sort(p+2,p+n+1);
45     s[++top]=p[1];
46     s[++top]=p[2];
47     for(i=3;i<=n;i++){
48         while((s[top]-s[top-1])*(p[i]-s[top-1])<=0) top--;//找到“更靠外的点”就舍弃栈顶的点
49         s[++top]=p[i];
50     }
51     s[top+1]=p[1];
52     for(i=1;i<=top;i++){
53         sum+=sqrt(dis(s[i],s[i+1]));//计算距离
54     }
55     return;
56 }
57 int main(){
58     n=read();
59     int i,j;
60     int x,y;
61     for(i=1;i<=n;i++){
62         p[i].x=read();p[i].y=read();
63     }
64     graham();
65     printf("%.2lf\n",sum);
66     return 0;
67 }
时间: 2024-08-24 21:04:52

bzoj1670 Usaco2006 Building the Moat护城河的挖掘 [凸包模板题]的相关文章

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

1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 Time Limit: 3 Sec  Memory Limit: 64 MB Submit: 387  Solved: 288 [Submit][Status][Discuss] Description 为了防止口渴的食蚁兽进入他的农场,Farmer John决定在他的农场周围挖一条护城河.农场里一共有N(8<=N<=5,000)股泉水,并且,护城河总是笔直地连接在河道上的相邻的两股泉水.护城河必须能保护

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

1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 Time Limit: 3 Sec  Memory Limit: 64 MB Submit: 628  Solved: 466 [Submit][Status][Discuss] Description 为了防止口渴的食蚁兽进入他的农场,Farmer John决定在他的农场周围挖一条护城河.农场里一共有N(8<=N<=5,000)股泉水,并且,护城河总是笔直地连接在河道上的相邻的两股泉水.护城河必须能保护

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

#include<cstdio> #include<cmath> #include<algorithm> using namespace std; #define N 5001 struct Point{int x,y;}p[N],bao[N]; bool operator < (Point a,Point b){return a.x!=b.x?a.x<b.x:a.y<b.y;} typedef long long ll; typedef Point

[bzoj1670][Usaco2006 Oct]Building the Moat

Description 为了防止口渴的食蚁兽进入他的农场,$Farmer John$决定在他的农场周围挖一条护城河.农场里一共有$N$股泉水,并且,护城河总是笔直地连接在河道上的相邻的两股泉水.护城河必须能保护所有的泉水,也就是说,能包围所有的泉水.泉水一定在护城河的内部,或者恰好在河道上.当然,护城河构成一个封闭的环. 挖护城河是一项昂贵的工程,于是,节约的$FJ$希望护城河的总长度尽量小.请你写个程序计算一下,在满足需求的条件下,护城河的总长最小是多少. 所有泉水的坐标都在范围为$(1..1

护城河的挖掘「USACO 2006」

题意 凸包模板,给定平面上点集,求包含所有点的凸包周长最小值. 思路 使用\(Graham\)扫描法解决. 考虑将最左下的点设为原点(事实上任意点均可作为原点),然后其余各点根据斜率排序. 对于每一个节点,我们考虑加入它是否会与已有的边点构成内凹,如果会,那么放弃已有边点. 显然我们可以通过维护一个单调栈完成该过程. 代码 #include <bits/stdc++.h> using namespace std; namespace StandardIO { template<typen

poj 2031 Building a Space Station【最小生成树prime】【模板题】

Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5699   Accepted: 2855 Description You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You ar

大神刷题表

9月27日 后缀数组:[wikioi3160]最长公共子串 dp:NOIP2001统计单词个数 后缀自动机:[spoj1812]Longest Common Substring II [wikioi3160]最长公共子串 [spoj7258]Lexicographical Substring Search 扫描线+set:[poj2932]Coneology 扫描线+set+树上删边游戏:[FJOI2013]圆形游戏 结论:[bzoj3706][FJ2014集训]反色刷 最小环:[poj1734

HDU 4667 Building Fence(求凸包的周长)

A - Building Fence Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Submit Status Description Long long ago, there is a famous farmer named John. He owns a big farm and many cows. There are two kinds of cows on his farm, o

POJ 3348 Cows [凸包 面积]

Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9022   Accepted: 3992 Description Your friend to the south is interested in building fences and turning plowshares into swords. In order to help with his overseas adventure, they are f