题解:P4286 [SHOI2008] 安全的航线

· · 题解

一开题想了想三分,最后因为实现太难放弃了。

本题目考虑 dfs。

大致思路如下:

考虑在一条线段上寻找答案,取其中点记为 mid,算终点离最近的陆地有多远,记录为 d,如果 mid 点在陆地内部则 d = 0。计这条线段的长度记为 L,当前已经得到的答案为 ans。

在这条线段上的点到最近陆地的距离一定在 [d-\frac{L}{2},d+\frac{L}{2}] 范围内,画图证明显然。如果 d+\frac{L}{2} \le ans 则这条线段没用,直接 return。如果 L 的值过低则无法对答案产生实质影响,也要 return。

如果没有 return 则将 ans 更新为 \max(ans,d),并将线段从中点分成两段递归下去。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define N 30
#define INF 0x3f3f3f3f
#define lowbit(x) (x&-x)
#define pii pair<point,point>
#define cpx complex<double>
#define poly vector<ll>
#define eps 1e-8
namespace Point{// 定义点和向量运算 
    struct point{double x,y;point(const double &x_ = 0,const double &y_ = 0){x = x_,y = y_;}};
    inline point operator+(const point &n1,const point &n2){return point(n1.x+n2.x,n1.y+n2.y);}
    inline point operator-(const point &n1,const point &n2){return point(n1.x-n2.x,n1.y-n2.y);}
    struct segment{point a,b;segment(point a_ = point(0,0),point b_ = point(0,0)){a = a_,b = b_;}};// 线段类
    inline double len(const point &n1){return sqrt(n1.x*n1.x+n1.y*n1.y);}
    inline double operator*(const point &n1,const point &n2){return n1.x*n2.x+n1.y*n2.y;}
    inline point mid_point(point a,point b){return point((a.x+b.x)/2,(a.y+b.y)/2);}
    inline double dst(point p,point a,point b){// 点 p 到线段 ab 的距离
        point ap = p-a,ab = b-a,bp = p-b;
        double t = ap*ab/(ab.x*ab.x+ab.y*ab.y);
        if(t < -eps)return len(ap);
        if(t - 1 > eps)return len(bp);
        point q = a + point(ab.x*t,ab.y*t);
        return len(p-q);
    }
}using namespace Point;
inline bool equal(const double &n1,const double &n2){return fabs(n1-n2)<=eps;}
double ans;
vector<point> a;// 路线
vector<segment> all;// 所有多边形的线段
int c,n,x,y,ln[N];
inline bool check(const point &p){// 判断 p 是否再陆地上
    int cnt = 0;
    double x1,y1,x2,y2,k,b;
    for(const segment sg:all){
        x1 = sg.a.x,x2 = sg.b.x,y1 = sg.a.y,y2 = sg.b.y;
        if(equal(x1,x2)){if(equal(x1,p.x))return 1;continue;}
        if(p.x - min(x1,x2) > eps && max(x2,x1)-p.x > eps){
            k = (y1 - y2)/(x1 - x2);b = y1 - k * x1;
            if(p.x*k+b - p.y > eps)++cnt;
        }
    }
    return cnt&1;
}
void dfs(point a,point b){
    double d,l = len(b-a);
    // if(l < 0.8 && check(a) && check(b))return ;
    point mid = mid_point(a,b);
    if(check(mid))d = 0;
    else{
        d = INF;
        for(const segment sg:all)d = min(d,dst(mid,sg.a,sg.b));
    }
    // printf("a = [%.2lf,%.2lf] b = [%.2lf,%.2lf] d = %.2lf l = %.2lf\n",a.x,a.y,b.x,b.y,d,l);
    if(ans - d - l/2 > eps || l < 1e-3)return ;
    ans = max(ans,d);dfs(a,mid),dfs(b,mid);
}

// 主函数
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin >> c >> n;a.reserve(n);
    for(int i = 1;i <= n;++i)cin >> x >> y,a.emplace_back(point(x,y));
    for(int i = 1;i <= c;++i){
        cin >> ln[i];cin >> x >> y;point a(x,y),b = a;
        for(int j = 2;j <= ln[i];++j){cin >> x >> y;
            all.emplace_back(segment(point(x,y),b)),b = point(x,y);
        }
        all.emplace_back(segment(a,b));
    }
    for(int i = 1;i < n;++i)dfs(a[i-1],a[i]);
    cout << fixed << setprecision(2) << ans;
    return 0;
}