【计算几何】 Codeforces Beta Round #67 (Div. 2) E. Ship's Shortest Path
读懂题意其实是模板题。就是细节略多。
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
#define EPS 0.00000001
struct Point
{
double x,y;
Point(){}
Point(const double &X,const double &Y){x=X;y=Y;}
double Length()
{
return sqrt(x*x+y*y);
}
}p[3],a[31];
struct data
{
Point p;
int I,J;
data(){}
data(const Point &P,const int &II,const int &JJ)
{
p=P;
I=II;
J=JJ;
}
}b[31];
int e;
typedef Point Vector;
Vector operator - (const Point &a,const Point &b)
{
return Vector(a.x-b.x,a.y-b.y);
}
Vector operator + (const Vector &a,const Vector &b)
{
return Vector(a.x+b.x,a.y+b.y);
}
double Cross(Vector a,Vector b)
{
return a.x*b.y-a.y*b.x;
}
Vector operator * (const double &K,const Vector &v)
{
return Vector(K*v.x,K*v.y);
}
Point GetIntersection(Point P,Vector v,Point Q,Vector w)
{
return P+(Cross(w,P-Q)/Cross(v,w))*v;
}
double dot(Vector a,Vector b)
{
return a.x*b.x+a.y*b.y;
}
bool InLine(Point a,Point P,Point Q)
{
if(fabs(Cross(a-P,Q-P))>=EPS)
return 0;
return dot(a-P,a-Q)<EPS;
}
int n;
int main()
{
// freopen("s.in","r",stdin);
double sum=0;
for(int i=0;i<2;++i)
scanf("%lf%lf",&p[i].x,&p[i].y);
scanf("%d",&n);
for(int i=0;i<n;++i)
scanf("%lf%lf",&a[i].x,&a[i].y);
for(int i=0;i<n;++i)
if(fabs(Cross(p[1]-a[(i+1)%n],a[i]-p[0]))<EPS)
{
printf("%.9lf\n",(p[1]-p[0]).Length());
return 0;
}
for(int i=0;i<n;++i)
sum+=(a[(i+1)%n]-a[i]).Length();
/*double sum2=0;
for(int k=i+1;k<j;++k)
sum2+=(a[(k+1)%n]-a[k]).Length();
sum2+=(a[i+1]-tp).Length();
sum2+=(tp2-a[j]).Length();
double td=min((tp-p[0]).Length()+(tp2-p[1]).Length(),
(tp2-p[0]).Length()+(tp-p[1]).Length());
printf("%.9lf\n",td+min(sum-sum2,min(sum2,(tp2-tp).Length()*2.0)));*/
for(int i=0;i<n;++i)
{
Point tp=GetIntersection(p[1],p[1]-p[0],a[i],a[(i+1)%n]-a[i]);
if(InLine(tp,a[i],a[(i+1)%n]) && InLine(tp,p[0],p[1]))
{
for(int j=i+1;j<n;++j)
{
Point tp2=GetIntersection(p[1],p[1]-p[0],a[j],a[(j+1)%n]-a[j]);
if(InLine(tp2,a[j],a[(j+1)%n]) && InLine(tp2,p[0],p[1]))
b[++e]=data(tp2,i,j);
}
for(int j=1;j<=e;++j)
if(fabs(b[j].p.x-tp.x)>=EPS || fabs(b[j].p.y-tp.y)>=EPS)
{
int I=b[j].I,J=b[j].J;
Point tp2=b[j].p;
double sum2=0;
for(int k=I+1;k<J;++k)
sum2+=(a[(k+1)%n]-a[k]).Length();
sum2+=(a[I+1]-tp).Length();
sum2+=(tp2-a[J]).Length();
double td=min((tp-p[0]).Length()+(tp2-p[1]).Length(),
(tp2-p[0]).Length()+(tp-p[1]).Length());
printf("%.9lf\n",td+min(sum-sum2,min(sum2,(tp2-tp).Length()*2.0)));
return 0;
}
int I=b[1].I,J=b[1].J;
Point tp2=b[1].p;
double sum2=0;
for(int k=I+1;k<J;++k)
sum2+=(a[(k+1)%n]-a[k]).Length();
sum2+=(a[I+1]-tp).Length();
sum2+=(tp2-a[J]).Length();
double td=min((tp-p[0]).Length()+(tp2-p[1]).Length(),
(tp2-p[0]).Length()+(tp-p[1]).Length());
printf("%.9lf\n",td+min(sum-sum2,min(sum2,(tp2-tp).Length()*2.0)));
return 0;
}
}
printf("%.9lf\n",(p[1]-p[0]).Length());
return 0;
}
【计算几何】 Codeforces Beta Round #67 (Div. 2) E. Ship's Shortest Path的更多相关文章
- Codeforces Beta Round #67 (Div. 2)
Codeforces Beta Round #67 (Div. 2) http://codeforces.com/contest/75 A #include<bits/stdc++.h> ...
- Codeforces Beta Round #67 (Div. 2)C. Modified GCD
C. Modified GCD time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
- Codeforces Beta Round #76 (Div. 2 Only)
Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...
- Codeforces Beta Round #75 (Div. 2 Only)
Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...
- Codeforces Beta Round #74 (Div. 2 Only)
Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...
随机推荐
- 自己模拟实现一下Google的赛马Doodle
今天的Google Doodle是个动态的,是一个骑马的动态Doodle,是谷歌纪念英国实验摄影师埃德沃德·迈布里奇182周年诞辰,埃德沃德·迈布里奇是运动摄影的开创者,所以谷歌涂鸦以一个运动的摄影作 ...
- [hdu 2298] 物理推导+二分答案
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2298 #include<bits/stdc++.h> using namespace st ...
- hibernate连接mysql,自动建表失败
hibernate的列名使用了mysql的关键字.
- 固定width但是有间隔
<!DOCTYPE > <html> <head> <title></title> <meta name="name&quo ...
- Spring学习-- Bean 的作用域
Bean 的作用域: 在 Spring 中 , 可以在 <bean> 元素的 scope 属性里设置 bean 的作用域. 默认情况下 , Spring 只为每个在 IOC 容器里声明的 ...
- 九大排序算法Java实现
之前学习数据结构与算法时花了三天时间整理九大排序算法,并采用Java语言来实现,今天第一次写博客,刚好可以把这些东西从总结的文档中拿出来与大家分享一下,同时作为自己以后的备忘录. 1.排序算法时间复杂 ...
- 斯特林数(Stirling number)
在组合数学,Stirling 数可指两类数,第一类Stirling 数和第二类 Stirling 数,都是由18世纪数学家 James Stirling 提出的. Stirling 数有两种,第一类和 ...
- Spring - IoC(5): 集合属性的注入
如果 Bean 的属性是个集合,则可以使用 <list/>.<set/>.<map/> 和 <props/> 元素向 List.Set.Map 和 Pr ...
- idea讲web项目部署到tomcat,热部署
idea是自动保存文件的,不需要ctrl+s手动保存. idea使用不习惯,修改了jsp文件后,刷新浏览器并没有立刻显示出来,而是要重新编译一下代码,重新部署才会出现. 在idea tomcat 中s ...
- 51nod 最大M子段和系列
1052 最大M子段和 N个整数组成的序列a[1],a[2],a[3],…,a[n],将这N个数划分为互不相交的M个子段,并且这M个子段的和是最大的.如果M >= N个数中正数的个数,那么输出所 ...