結果

問題 No.2376 障害物競プロ
ユーザー poyonpoyon
提出日時 2023-06-20 00:41:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,039 bytes
コンパイル時間 3,300 ms
コンパイル使用メモリ 222,912 KB
実行使用メモリ 14,680 KB
最終ジャッジ日時 2023-09-23 07:38:07
合計ジャッジ時間 69,877 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 244 ms
4,376 KB
testcase_05 AC 347 ms
4,376 KB
testcase_06 AC 151 ms
4,380 KB
testcase_07 AC 404 ms
4,380 KB
testcase_08 AC 421 ms
4,504 KB
testcase_09 AC 396 ms
4,404 KB
testcase_10 AC 383 ms
4,384 KB
testcase_11 AC 311 ms
4,384 KB
testcase_12 AC 268 ms
4,384 KB
testcase_13 AC 420 ms
4,456 KB
testcase_14 AC 399 ms
4,384 KB
testcase_15 AC 373 ms
4,388 KB
testcase_16 AC 409 ms
4,408 KB
testcase_17 AC 299 ms
4,380 KB
testcase_18 AC 261 ms
4,384 KB
testcase_19 AC 2,712 ms
10,380 KB
testcase_20 AC 2,668 ms
10,132 KB
testcase_21 AC 2,696 ms
10,360 KB
testcase_22 AC 346 ms
4,384 KB
testcase_23 AC 203 ms
4,384 KB
testcase_24 AC 241 ms
4,384 KB
testcase_25 AC 120 ms
4,384 KB
testcase_26 AC 273 ms
4,384 KB
testcase_27 AC 243 ms
4,384 KB
testcase_28 AC 139 ms
4,384 KB
testcase_29 AC 124 ms
4,384 KB
testcase_30 AC 108 ms
4,384 KB
testcase_31 AC 147 ms
4,384 KB
testcase_32 AC 22 ms
4,380 KB
testcase_33 AC 52 ms
4,384 KB
testcase_34 AC 84 ms
4,384 KB
testcase_35 AC 49 ms
4,384 KB
testcase_36 AC 238 ms
4,388 KB
testcase_37 AC 323 ms
4,384 KB
testcase_38 AC 119 ms
4,384 KB
testcase_39 AC 302 ms
4,388 KB
testcase_40 AC 79 ms
4,444 KB
testcase_41 AC 113 ms
4,388 KB
testcase_42 TLE -
testcase_43 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

// 嘘解法 (TLE)
// 毎回ダイクストラ + 枝刈り(終点に達したら終了) + 答えをメモ化
// ローカルでは test_21 で 3.3 s (< 4 s) なので間に合っている

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

struct Point {
    ll x, y;
    Point(ll x = 0, ll y = 0) : x(x), y(y) {}
    Point& operator+=(const Point& v) noexcept {
        x += v.x;
        y += v.y;
        return *this;
    }
    Point& operator-=(const Point& v) noexcept {
        x -= v.x;
        y -= v.y;
        return *this;
    }
    Point operator+(const Point& v) const noexcept { return Point(*this) += v; }
    Point operator-(const Point& v) const noexcept { return Point(*this) -= v; }
};

ll norm(const Point& a) { return a.x * a.x + a.y * a.y; }
ll dot(const Point& a, const Point& b) { return a.x * b.x + a.y * b.y; }
ll cross(const Point& a, const Point& b) { return a.x * b.y - a.y * b.x; }

int ccw(const Point& p0, const Point& p1, const Point& p2) {
    Point a = p1 - p0;
    Point b = p2 - p0;
    if (cross(a, b) > 0) { return 1; }
    if (cross(a, b) < 0) { return -1; }
    if (dot(a, b) < 0) { return 2; }
    if (norm(a) < norm(b)) { return -2; }
    return 0;
}

bool is_intersected(const Point& p1, const Point& p2, const Point& p3, const Point& p4) {
    return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0) &&
           (ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0);
}
bool on_segment(const Point& p1, const Point& p2, const Point& p) {
    return ccw(p1, p2, p) == 0;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int N, M;
    cin >> N >> M;
    vector<vector<int>> x(N, vector<int>(2));
    vector<vector<int>> y(N, vector<int>(2));
    for (int i = 0; i < N; i++) {
        cin >> x[i][0] >> y[i][0] >> x[i][1] >> y[i][1];
    }

    int N2 = N * 2;
    int inf = (1 << 30);
    vector<vector<pair<int, double>>> G(N2);
    for (int i = 0; i < N2; i++) {
        int a = i / 2;
        int b = i % 2;
        int x1 = x[a][b];
        int y1 = y[a][b];
        Point p1(x1, y1);
        for (int j = i + 1; j < N2; j++) {
            int c = j / 2;
            int d = j % 2;
            int x2 = x[c][d];
            int y2 = y[c][d];
            Point p2(x2, y2);
            bool ok = [&]() -> bool {
                for (int k = 0; k < N; k++) {
                    if (k == a || k == c) { continue; }
                    Point p3(x[k][0], y[k][0]);
                    Point p4(x[k][1], y[k][1]);
                    if (is_intersected(p1, p2, p3, p4)) { return false; }
                }
                return true;
            }();
            if (ok) {
                auto w = hypot(x2 - x1, y2 - y1);
                G[i].emplace_back(j, w);
                G[j].emplace_back(i, w);
            }
        }
    }

    map<pair<int, int>, double> memo;
    for (int i = 0; i < M; i++) {
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        a--;
        b--;
        c--;
        d--;

        int s = 2 * a + b;
        int t = 2 * c + d;
        double ans = inf;

        auto it = memo.find({s, t});
        if (it != memo.end()) {
            ans = it->second;
            cout << ans << '\n';
            continue;
        }

        vector<double> dist(N2, inf);
        priority_queue<pair<double, int>, vector<pair<double, int>>, greater<pair<double, int>>> pq;
        dist[s] = 0;
        pq.emplace(dist[s], s);
        while (not pq.empty()) {
            auto [min_dist, u] = pq.top();
            pq.pop();
            if (u == t) {
                ans = min_dist;
                break;
            }
            if (dist[u] < min_dist) { continue; }
            for (const auto& [v, w] : G[u]) {
                if (dist[v] > min_dist + w) {
                    dist[v] = min_dist + w;
                    pq.emplace(dist[v], v);
                }
            }
        }
        memo[{s, t}] = ans;
        memo[{t, s}] = ans;
        cout << ans << '\n';
    }

    return 0;
}
0