[Usaco2006 Oct] Building the Moat护城河的挖掘
来源:互联网 发布:javascript图片轮播 编辑:程序博客网 时间:2024/04/30 20:53
题目描述
为了防止口渴的食蚁兽进入他的农场,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。
输入格式
- 第1行: 一个整数,N * 第2..N+1行: 每行包含2个用空格隔开的整数,x[i]和y[i],即第i股泉水的位 置坐标
输出格式
- 第1行: 输出一个数字,表示满足条件的护城河的最短长度。保留两位小数
样例数据
样例输入
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
样例输出
70.87
题目分析
裸凸包
源代码
#include<algorithm>#include<iostream>#include<iomanip>#include<cstring>#include<cstdlib>#include<vector>#include<cstdio>#include<cmath>#include<queue>using namespace std;inline const int Get_Int() { int num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj;}const double eps=1e-10;int DoubleCompare(double x) { //精度三出口判断与0关系 if(fabs(x)<eps)return 0; //=0 else if(x<0)return -1; //<0 else return 1; //>0}struct Point { double x,y; bool operator < (Point b) const { return x<b.x||(x==b.x&&y<b.y); }};double Dist(Point a,Point b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}struct Vector { double x,y;};Vector operator - (Point a,Point b) { Vector c; c.x=b.x-a.x; c.y=b.y-a.y; return c;}double Cross(Vector a,Vector b) { //叉积 return a.x*b.y-b.x*a.y;}double Area(Point a,Point b,Point c) { //三点的平行四边形有向面积 Vector u=b-a; Vector v=c-a; return Cross(u,v);}double Area(int n,Point* P) { //计算多边形有向面积(剖分法) double ans=0; for(int i=2; i<n; i++)ans+=Area(P[1],P[i],P[i+1]); return ans/2;}int ConvexHull(int n,Point* p,Point* ans) { //Andrew算法求凸包(求上链与下链):p是点数组,ch是凸包顶点,返回顶点数 //输入不能有重复点,若要凸包边上没有输入点,将两个<=改为< sort(p+1,p+n+1); int top=0; for(int i=1; i<=n; i++) { while(top>1&&DoubleCompare(Cross(ans[top]-ans[top-1],p[i]-ans[top-1]))<=0)top--; ans[++top]=p[i]; } int k=top; for(int i=n-1; i>=1; i--) { while(top>k&&DoubleCompare(Cross(ans[top]-ans[top-1],p[i]-ans[top-1]))<=0)top--; ans[++top]=p[i]; } if(n>1)top--; return top;}//////////////Point a[10005],b[10005];int n;int main() { n=Get_Int(); for(int i=1; i<=n; i++)scanf("%lf%lf",&a[i].x,&a[i].y); int cnt=ConvexHull(n,a,b); double s=Area(cnt,b),l; for(int i=1; i<cnt; i++)l+=Dist(b[i],b[i+1]); l+=Dist(b[cnt],b[1]); if(s==0)l/=2; //多点共线 printf("%0.2lf\n",l); return 0;}
0 0
- bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘
- 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘
- 【bzoj1670】[Usaco2006 Oct]Building the Moat护城河的挖掘 求凸包
- bzoj1670【Usaco2006 Oct】Building the Moat 护城河的挖掘
- 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘
- BZOJ 1670 [Usaco2006 Oct]Building the Moat护城河的挖掘
- [Usaco2006 Oct] Building the Moat护城河的挖掘
- BZOJ 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘
- bzoj1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 计算几何 凸包
- 【bzoj1670】[Usaco2006 Oct]Building the Moat护城河的挖掘 凸包
- BZOJ 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘|二维凸包
- 【BZOJ 1670】[Usaco2006 Oct]Building the Moat护城河的挖掘 裸凸包
- [BZOJ1670][Usaco2006 Oct]Building the Moat护城河的挖掘(凸包)
- bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 (凸包)
- BZOJ 1670 [Usaco2006 Oct]Building the Moat护城河的挖掘 凸包
- bzoj1670[Usaco2006 Oct]Building the Moat护城河的挖掘 凸包(Andrew)
- bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘(凸包)
- [凸包]BZOJ 1670——[Usaco2006 Oct]Building the Moat护城河的挖掘
- ml-day02
- EXCEL 常用表达式
- 手动配置easyx头文件和库文件到vs2010中
- c 一些宏的使用
- ZCMU-1808-FJ的字符串
- [Usaco2006 Oct] Building the Moat护城河的挖掘
- Android开发实践:拷贝assets到sdcard
- BZOJ 1044: [HAOI2008]木棍分割 DP,前缀和优化,二分答案
- 读《黑客与画家》感想(2)
- linux编译busybox,uboot,kernel等常用到的命令及出现过的问题
- 韩信点兵
- ZCMU-1841-完美的代价
- [SDOI2012] 最近最远点对
- 使用Retrofit和Rxjava下载启动图图片