题目描述
题解
这道题路径的交点处是可以随意通行的
如果1->n是可通行的那么直接走就行了
如果不能通行
对于每一个点i,找出它能直接走到的编号最大的点,显然只有这个点是有用的
然后从点i向这个点连一条直线,加上n->1这条直线,实际上交出了一个凸多边形
答案即为这个凸多边形的边长-1->n的路径长
代码
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define N 1000005
const double eps=1e-9;
const double inf=1e7;
int dcmp(double x)
{
if (x<=eps&&x>=-eps) return 0;
return (x>0)?1:-1;
}
struct data
{
int x,y;
bool operator < (const data &a) const
{
return x<a.x||x==a.x&&y>a.y;
}
}e[N];
struct Vector
{
double x,y;
Vector(double X=0,double Y=0,double ANG=0)
{
x=X,y=Y;
}
};
typedef Vector Point;
struct Line
{
Point p;
Vector v;
double ang;
Line(Point P=Point(0,0),Vector V=Vector(0,0),double ANG=0)
{
p=P,v=V,ang=atan2(v.y,v.x);
}
bool operator < (const Line &a) const
{
return ang<a.ang;
}
};
Vector operator + (Vector a,Vector b) {return Vector(a.x+b.x,a.y+b.y);}
Vector operator - (Vector a,Vector b) {return Vector(a.x-b.x,a.y-b.y);}
Vector operator * (Vector a,double b) {return Vector(a.x*b,a.y*b);}
int n,m,cnt,l,r,est[N];
double ans;
Point pt[N],p[N],poly[N];
Line L[N],q[N];
bool flag;
double Len(Vector a)
{
return sqrt(a.x*a.x+a.y*a.y);
}
double Cross(Vector a,Vector b)
{
return a.x*b.y-a.y*b.x;
}
Point GLI(Point P,Vector v,Point Q,Vector w)
{
Vector u=P-Q;
double t=Cross(w,u)/Cross(v,w);
return P+v*t;
}
bool Onleft(Line m,Point P)
{
Vector w=P-m.p;
return dcmp(Cross(m.v,w))>=0;
}
void halfp()
{
sort(L+1,L+cnt+1);
q[l=r=1]=L[1];
for (int i=2;i<=cnt;++i)
{
while (l<r&&!Onleft(L[i],p[r-1]))
--r;
while (l<r&&!Onleft(L[i],p[l]))
++l;
q[++r]=L[i];
if (dcmp(Cross(q[r].v,q[r-1].v))==0)
{
--r;
if (Onleft(q[r],L[i].p))
q[r]=L[i];
}
if (l<r)
p[r-1]=GLI(q[r-1].p,q[r-1].v,q[r].p,q[r].v);
}
while (l<r&&!Onleft(q[l],p[r-1]))
--r;
cnt=0;
if (r-l<=1) return;
p[r]=GLI(q[r].p,q[r].v,q[l].p,q[l].v);
for (int i=l;i<=r;++i) poly[++cnt]=p[i];
}
int main()
{
scanf("%d%d",&n,&m);
for (int i=1;i<=n;++i) scanf("%lf%lf",&pt[i].x,&pt[i].y);
for (int i=1;i<=m;++i)
{
scanf("%d%d",&e[i].x,&e[i].y);
if (e[i].x>e[i].y) swap(e[i].x,e[i].y);
if (e[i].x==1&&e[i].y==n) flag=1;
}
if (!flag)
{
ans=Len(pt[1]-pt[n]);
printf("%.9lf\n",ans);
}
sort(e+1,e+m+1);
for (int i=1;i<=n;++i) est[i]=n+1;
int now=1;
while (now<=m)
{
int i;
for (i=now;e[i].x==e[now].x;++i)
{
if (i==now&&e[i].y!=n) {est[e[now].x]=n;break;}
else if (i!=now&&e[i].y+1!=e[i-1].y) {est[e[now].x]=e[i-1].y-1;break;}
}
for (;e[i].x==e[now].x;++i);
if (est[e[now].x]==n+1)
{
est[e[now].x]=e[i-1].y-1;
if (e[now].x==e[i-1].y-1) --est[e[now].x];
}
now=i;
}
L[++cnt]=Line(Point(inf,inf),Vector(-2*inf,0));
L[++cnt]=Line(Point(-inf,inf),Vector(0,-2*inf));
L[++cnt]=Line(Point(-inf,-inf),Vector(2*inf,0));
L[++cnt]=Line(Point(inf,-inf),Vector(0,2*inf));
L[++cnt]=Line(pt[1],pt[n]-pt[1]);
for (int i=1;i<n;++i)
{
if (est[i]==n+1) est[i]=n;
if (est[i]&&est[i]>i)
L[++cnt]=Line(pt[est[i]],pt[i]-pt[est[i]]);
}
halfp();
for (int i=1;i<=cnt;++i)
ans+=Len(poly[i%cnt+1]-poly[(i+1)%cnt+1]);
ans-=Len(pt[1]-pt[n]);
printf("%.9lf\n",ans);
}