BZOJ 1137 POI2009 Wsp 岛屿 半平面交

题目大意:给定一个凸 n 边形,从点1走到点 n <script type="math/tex" id="MathJax-Element-97">n</script>,有一些边不能走,若两条边相交可以变道,求最短路

MD这水题看错题困扰了我多年= =
一直以为是补图的最短路……

最短路显然是半平面交
从一个点出发的所有边中只有最后一条可能在半平面交上
然后就完事了啊= =

#include <cmath>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define M 100100
#define EPS 1e-7
using namespace std;
struct Point{
    double x,y;
    Point() {}
    Point(double _,double __):
        x(_),y(__) {}
    friend istream& operator >> (istream &_,Point &p)
    {
        scanf("%lf%lf",&p.x,&p.y);
        return _;
    }
    friend Point operator + (const Point &p1,const Point &p2)
    {
        return Point(p1.x+p2.x,p1.y+p2.y);
    }
    friend Point operator - (const Point &p1,const Point &p2)
    {
        return Point(p1.x-p2.x,p1.y-p2.y);
    }
    friend double operator * (const Point &p1,const Point &p2)
    {
        return p1.x*p2.y-p1.y*p2.x;
    }
    friend Point operator * (const Point &p,double rate)
    {
        return Point(p.x*rate,p.y*rate);
    }
    friend double Distance (const Point &p1,const Point &p2)
    {
        return sqrt( (p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y) ) ;
    }
}points[M];
struct Line{
    Point p,v;
    Line() {}
    Line(const Point &_,const Point &__):
        p(_),v(__) {}
    friend Point Get_Intersection(const Line &l1,const Line &l2)
    {
        Point u=l1.p-l2.p;
        double temp=(l2.v*u)/(l1.v*l2.v);
        return l1.p+l1.v*temp;
    }
    friend bool On_Left(const Line &l,const Point &p)
    {
        return (l.p-p)*l.v > EPS ;
    }
}stack[M];int top;
int n,m,now;
vector<int> a[M];
double ans;
void Insert(const Line &l)
{
    while( top>=2 && On_Left(l,Get_Intersection(stack[top-1],stack[top])) )
        top--;
    stack[++top]=l;
}
int main()
{
    int i,j,k,x,y;
    cin>>n>>m;
    for(i=1;i<=n;i++)
        cin>>points[i];
    for(i=1;i<=m;i++)
    {
        scanf("%d%d",&x,&y);
        if(x>y) swap(x,y);
        a[x].push_back(y);
    }
    for(i=1;i<=n;i++)
        sort(a[i].begin(),a[i].end());
    for(i=1;i<=n;i++)
    {
        for(j=n,k=a[i].size()-1;j>now&&k>=0;j--,k--)
            if(a[i][k]!=j)
                break;
        if(j>now)
            Insert(Line(points[i],points[now=j]-points[i]));
    }
    Point last=points[1];
    for(i=2;i<=top;i++)
    {
        Point temp=Get_Intersection(stack[i-1],stack[i]);
        ans+=Distance(last,temp);
        last=temp;
    }
    ans+=Distance(last,points[n]);
    printf("%.9lf\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值