bzoj1670 Usaco2006 Building the Moat护城河的挖掘 [凸包模板题]
2016-06-08 22:28
417 查看
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
202 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
/**/ #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #define ll long long using namespace std; const int mxn=10000; int n; struct P{ int x,y; }p[mxn],s[mxn]; int top=0; double sum=0; inline int read(){ int x=0,f=1; char ch=getchar(); 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; } inline P operator -(P a,P b){ P t; t.x=a.x-b.x; t.y=a.y-b.y; return t; } inline ll operator *(P a,P b){ return a.x*b.y-a.y*b.x; } inline ll dis(P a,P b){ return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y); } inline bool operator < (P a,P b){ ll t=(a-p[1])*(b-p[1]); if(t==0)return dis(p[1],a)<dis(p[1],b); return t>0; } void graham(){ int t=1,i; for(int i=2;i<=n;i++) if(p[i].y<p[t].y || (p[i].y==p[t].y&&p[i].x<p[t].x))t=i;//找出y值最小点作为起点 swap(p[1],p[t]); sort(p+2,p+n+1); s[++top]=p[1]; s[++top]=p[2]; for(i=3;i<=n;i++){ while((s[top]-s[top-1])*(p[i]-s[top-1])<=0) top--;//找到“更靠外的点”就舍弃栈顶的点 s[++top]=p[i]; } s[top+1]=p[1]; for(i=1;i<=top;i++){ sum+=sqrt(dis(s[i],s[i+1]));//计算距离 } return; } int main(){ n=read(); int i,j; int x,y; for(i=1;i<=n;i++){ p[i].x=read();p[i].y=read(); } graham(); printf("%.2lf\n",sum); return 0; }
相关文章推荐
- 使用NtUserBuildHwndList遍历窗口_win7x64
- mysql 慢查询分析工具:pt-query-digest 在mac 上的安装使用
- 1048 - Conquering Keokradong
- squirrel-sql通过phoenix-4.7.0连接windows中的hbase-1.1.3(二)
- UVA 11527 Unique Snowflakes
- poj 3061 Subsequence
- ( Leetcode 95 ) Unique Binary Search Trees II
- UIImagePickerController.h SDK 翻译
- UILabel 高度的自适应与获取
- QtQuick折腾结论之model delegate 的正确使用姿势
- IOS 多线程02-pthread 、 NSThread 、GCD 、NSOperationQueue、NSRunLoop
- QtQuick排坑(不定时更新)
- POJ 2524 Ubiquitous Religions
- 2016 UESTC Training for Math Problem E- 蹲在墙角画圈圈的愚人王
- LeetCode:N-Queens
- 在Qt中生成UUID(全球唯一码)
- vue 多项目 依赖webpack开发共用 配置
- poj1458Common Subsequence--LCS
- LeetCode:N-Queens II
- 有关StringBuilder字符串的一些应用