bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘
2016-03-05 09:48
441 查看
Submit: 347 Solved: 255
[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
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 题解: 一道裸的求凸包周长。讲解:Graham's Scan法求解凸包问题#include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cmath> #include<cstring> #include<queue> #include<vector> using namespace std; typedef long long LL; const int inf=1e9; const double eps=1e-7; int N,top; double ans; struct P{ int x,y; friend P operator-(P a,P b){ P t; t.x=a.x-b.x; t.y=a.y-b.y; return t; } friend double operator*(P a,P b){//叉积 return a.x*b.y-b.x*a.y; } friend double dis(P a,P b){ return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y); } }p[5005],stac[5005]; inline bool operator<(P a,P b){ double t=(a-p[1])*(b-p[1]);//向量p[1]a和向量p[1]b 求叉积 if(fabs(t)<=eps) return dis(p[1],a)<dis(p[1],b);//如果斜率相等,长度越短越小 fabs()一定要有 return t>eps;//t>0 表示a在b的顺时针方向 } inline void graham(){ int tmp=1; for(int i=2;i<=N;i++){ if(p[i].y<p[tmp].y||(p[i].y==p[tmp].y&&p[i].x<p[tmp].x)) tmp=i; } swap(p[1],p[tmp]);//找到极点 sort(p+2,p+N+1);//顺时针极角排序 stac[++top]=p[1]; stac[++top]=p[2]; for(int i=3;i<=N;i++){ while((stac[top]-stac[top-1])*(p[i]-stac[top-1])<=0) top--; stac[++top]=p[i]; } stac[top+1]=p[1]; for(int i=1;i<=top;i++){ ans+=sqrt(dis(stac[i],stac[i+1])); } } int main(){ scanf("%d",&N); for(int i=1;i<=N;i++) scanf("%d%d",&p[i].x,&p[i].y); graham(); printf("%.2lf",ans); return 0; }
相关文章推荐
- 62. Unique Paths
- 60. Permutation Sequence
- NSData与UIImage之间的相互转换
- EasyUi的基础学习和总结
- ngRoute 和 ui.router 的使用方法和区别
- iOS学习—UIFont字体
- iOS UI设计---九宫格布局算法
- serialVersionUID 作用
- iOS UITableView使用练习 —— HERO博客
- 炫彩界面库v1.4.2-360安全卫士v8.5-UI设计器使用教程
- DirectUI 2D/3D 界面库集合 分析之总结
- IOS UIPrintInteractionController 打印
- UVA 11324 The Largest Clique(SCC+dp)
- 【杭电】[1509]Windows Message Queue
- druid错误解决办法
- Druid连接池简单入门配置
- leetcode 334 : Increasing Triplet Subsequence : 被智商压制
- 【CodeForces】[612C]Replace To Make Regular Bracket Sequence
- 深入Android的消息机制源码详解~Handler,MessageQueue与Looper关系
- druid removeAbandoned 含义 和使用