結果

問題 No.2855 Move on Grid
ユーザー SnowBeenDidingSnowBeenDiding
提出日時 2024-08-25 14:02:30
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,867 bytes
コンパイル時間 3,520 ms
コンパイル使用メモリ 265,788 KB
実行使用メモリ 11,624 KB
最終ジャッジ日時 2024-08-25 14:03:01
合計ジャッジ時間 29,657 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 625 ms
11,240 KB
testcase_11 AC 682 ms
11,244 KB
testcase_12 AC 676 ms
11,216 KB
testcase_13 AC 664 ms
11,088 KB
testcase_14 AC 636 ms
11,180 KB
testcase_15 AC 671 ms
11,236 KB
testcase_16 AC 638 ms
11,104 KB
testcase_17 AC 667 ms
11,084 KB
testcase_18 AC 666 ms
11,092 KB
testcase_19 AC 702 ms
11,348 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 722 ms
11,488 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
using namespace std;

typedef long long ll;

template <typename T> struct Graph {
    struct edge {
        int to;
        T cost;
    };
    int N;
    vector<vector<edge>> G;
    vector<T> dist;
    vector<int> prever;

    Graph(int n) { init(n); }
    T inf() {
        if (is_same_v<T, int>)
            return 1e9;
        else
            return 1e18;
    }
    T zero() { return T(0); }

    void init(int n) {
        N = n;
        G.resize(N);
        dist.resize(N, inf());
    }
    void add_edge(int s, int t, T cost) {
        edge e;
        e.to = t, e.cost = cost;
        G[s].push_back(e);
    }
    void dijkstra(int s) {
        rep(i, 0, N) dist[i] = inf();
        prever = vector<int>(N, -1);
        dist[s] = zero();
        priority_queue<pair<T, int>, vector<pair<T, int>>,
                       greater<pair<T, int>>>
            q;
        q.push({zero(), s});
        while (!q.empty()) {
            int now;
            T nowdist;
            tie(nowdist, now) = q.top();
            q.pop();
            if (dist[now] < nowdist)
                continue;
            for (auto e : G[now]) {
                T nextdist = nowdist + e.cost; // 次の頂点への距離
                if (dist[e.to] > nextdist) {
                    prever[e.to] = now;
                    dist[e.to] = nextdist;
                    q.push({dist[e.to], e.to});
                }
            }
        }
    }

    vector<int> get_path(int t) { // tへの最短路構築
        if (dist[t] >= inf())
            return {-1};
        vector<int> path;
        for (; t != -1; t = prever[t]) {
            path.push_back(t);
        }
        reverse(path.begin(), path.end());
        return path;
    }
};

int main() {
    int h, w, k;
    cin >> h >> w >> k;
    vector a(h, vector<int>(w));
    rep(i, 0, h) rep(j, 0, w) cin >> a[i][j];
    auto check = [&](int mid) { // ([...,1,1,1,0,0,0,...])
        Graph<int> gr(h * w);
        rep(i, 0, h) rep(j, 0, w) {
            rep(k, 0, 4) {
                int ni = i + "1012"[k] - '1';
                int nj = j + "2101"[k] - '1';
                if (ni < 0 || ni >= h || nj < 0 || nj >= w)
                    continue;
                int cost = a[i][j] < mid || a[ni][nj] < mid;
                gr.add_edge(i * w + j, ni * w + nj, cost);
            }
        }
        gr.dijkstra(0);
        return gr.dist[h * w - 1] <= k;
    };
    auto binary = [&]() {      // ll
        ll L = 0, R = 1e9 + 1; // 解[L,R) 探索範囲(L,R)
        ll mid = L + (R - L) / 2;
        while (R - L > 1) {
            if (check(mid))
                L = mid;
            else
                R = mid;
            mid = L + (R - L) / 2;
        }
        return L; // L-true R-false
    };
    cout << binary() << endl;
}
0