2020ICPC·小米 网络选拔赛第一场 B.Intelligent Robot 最短路+计算几何

原题链接

在这里插入图片描述
题意:给定起点和终点还有一些线段,要求不能穿越线段从起点到终点的最短路径。

注意线段的端点是可以选的,而且可以沿着线段走。

考虑到k的范围只有300,因此考虑到n^3的枚举,我们可以枚举每两个点,然后再根据k条线判断是否相交,如果都不相交,那么就可以在两点之间连一条线了。

至于判断是否相交,我们可以用向量叉积的方法,详细证明就不多说了。

#include <bits/stdc++.h>
using namespace std;
#define ACM_LOCAL
typedef long long ll;
const ll MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int N = 1e6 + 10;
const double eps = 1e-6;

int stx, sty, edx, edy;
int n, m, k, idx;

struct Point {
    
    
    double x,y;
    Point(){
    
    }
    Point(double x,double y) {
    
    
        this -> x = x;
        this -> y = y;
    }
    Point operator-(Point a)
    {
    
    
        return Point(x - a.x,y - a.y);
    }
    double operator*(Point a)
    {
    
    
        return x * a.y - y * a.x;
    }
};

double get_dis(Point p1,Point p2) {
    
    
    return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}

struct Line {
    
    
    Point a,b;
}l[N];

bool judge(Line line1, Line line2) {
    
     
    Point AD = line1.a - line2.a;
    Point AC = line1.a - line2.b;
    Point AB = line1.a - line1.b;

    Point CA = line2.a - line1.a;
    Point CB = line2.a - line1.b;
    Point CD = line2.a - line2.b;
    if((CD * CA) * (CD * CB) < 0 && (AB * AC) * (AB * AD) < 0) 
        return true;
    return false;
}

struct edge {
    
    
    int to, next;
    double w;
}e[N<<1];
int cnt, h[N], vis[N];
double dis[N];
void add(int u, int v, double w) {
    
    
    e[cnt].to = v;
    e[cnt].next = h[u];
    e[cnt].w = w;
    h[u] = cnt++;
}

struct node {
    
    
    int id;
    double dis;
    bool operator < (const node &rhs) const {
    
    
        return dis > rhs.dis;
    }
};

void dij(int st, int ed) {
    
    
    priority_queue<node> q;
    for (int i = 0; i <= ed; i++) dis[i] = INF;
    dis[st] = 0;
    q.push({
    
    st, 0});
    while (q.size()) {
    
    
        int u = q.top().id;
        q.pop();
        if (vis[u]) continue;
        vis[u] = 1;
        for (int i = h[u]; ~i; i = e[i].next) {
    
    
            int v = e[i].to;
            if (dis[v] > dis[u] + e[i].w) {
    
    
                dis[v] = dis[u] + e[i].w;
                if (!vis[v]) {
    
    
                    q.push({
    
    v, dis[v]});
                }
            }
        }
    }
}

void solve() {
    
    
    vector<Point> vcc;
    vcc.push_back({
    
    0, 0});
    scanf("%d %d %d", &n, &m, &k);
    memset(h, -1, sizeof h);
    for (int i = 1; i <= k; i++) {
    
    
        Point a1, a2;
        scanf("%lf%lf", &a1.x, &a1.y);
        scanf("%lf%lf", &a2.x, &a2.y);
        l[i].a = a1;
        l[i].b = a2;
        vcc.push_back(a1);
        vcc.push_back(a2);
        add(2*i-1, 2*i, get_dis(a1, a2));
        add(2*i-1, 2*i, get_dis(a1, a2));
    }
    Point st, ed;
    scanf("%lf%lf", &st.x, &st.y);
    scanf("%lf%lf", &ed.x, &ed.y);
    vcc.push_back(st);
    vcc.push_back(ed);

    for (int i = 1; i <= k*2+2; i++) {
    
    
        for (int j = i+1; j <= k*2+2; j++) {
    
    
            Line line1 = {
    
    vcc[i], vcc[j]};
            int f = 0;
            for (int o = 1; o <= k; o++) {
    
    
                if (judge(line1, l[o])) {
    
    
                    f = 1;
                    break;
                }
            }
            if (!f) add(i, j, get_dis(vcc[i], vcc[j])), add(j, i, get_dis(vcc[i], vcc[j]));
        }
    }
    dij(k*2+1, k*2+2);
    printf("%.4lf\n", dis[k*2+2]);
}

signed main() {
    
    
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifdef ACM_LOCAL
    freopen("input", "r", stdin);
    freopen("output", "w", stdout);
#endif
    solve();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/kaka03200/article/details/109384892