結果

問題 No.2855 Move on Grid
ユーザー ynishi2015ynishi2015
提出日時 2024-08-25 14:40:28
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 833 ms / 3,000 ms
コード長 2,130 bytes
コンパイル時間 1,370 ms
コンパイル使用メモリ 119,576 KB
実行使用メモリ 15,060 KB
最終ジャッジ日時 2024-08-25 14:40:56
合計ジャッジ時間 24,786 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 152 ms
6,816 KB
testcase_01 AC 224 ms
7,280 KB
testcase_02 AC 138 ms
6,940 KB
testcase_03 AC 94 ms
6,944 KB
testcase_04 AC 69 ms
6,944 KB
testcase_05 AC 146 ms
6,940 KB
testcase_06 AC 139 ms
6,940 KB
testcase_07 AC 14 ms
6,940 KB
testcase_08 AC 124 ms
6,944 KB
testcase_09 AC 57 ms
6,940 KB
testcase_10 AC 589 ms
14,032 KB
testcase_11 AC 638 ms
14,032 KB
testcase_12 AC 637 ms
14,036 KB
testcase_13 AC 605 ms
14,032 KB
testcase_14 AC 639 ms
14,032 KB
testcase_15 AC 626 ms
14,016 KB
testcase_16 AC 597 ms
14,072 KB
testcase_17 AC 590 ms
14,076 KB
testcase_18 AC 626 ms
14,036 KB
testcase_19 AC 593 ms
14,036 KB
testcase_20 AC 724 ms
14,164 KB
testcase_21 AC 718 ms
14,932 KB
testcase_22 AC 704 ms
14,928 KB
testcase_23 AC 682 ms
14,164 KB
testcase_24 AC 710 ms
14,928 KB
testcase_25 AC 769 ms
14,936 KB
testcase_26 AC 676 ms
14,168 KB
testcase_27 AC 705 ms
15,060 KB
testcase_28 AC 676 ms
14,908 KB
testcase_29 AC 730 ms
14,932 KB
testcase_30 AC 833 ms
14,964 KB
testcase_31 AC 745 ms
14,968 KB
testcase_32 AC 784 ms
14,804 KB
testcase_33 AC 748 ms
14,844 KB
testcase_34 AC 788 ms
14,932 KB
testcase_35 AC 758 ms
15,056 KB
testcase_36 AC 663 ms
14,932 KB
testcase_37 AC 773 ms
14,932 KB
testcase_38 AC 789 ms
15,032 KB
testcase_39 AC 726 ms
14,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <climits>

using namespace std;

class Di {
public:
    vector<long long> distance;
    vector<vector<pair<int, long long>>> G;
    long long inf = LLONG_MAX;

    Di(int n) {
        distance.assign(n + 1, inf);
        G.resize(n + 1);
    }

    void connect(int from, int to, long long cost) {
        G[from].emplace_back(to, cost);
    }

    void solve(int from) {
        priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;
        distance[from] = 0;
        pq.emplace(0, from);

        while (!pq.empty()) {
            auto [dist, f] = pq.top();
            pq.pop();

            if (dist > distance[f]) continue;

            for (auto &[t, cost] : G[f]) {
                long long new_dist = distance[f] + cost;
                if (distance[t] > new_dist) {
                    distance[t] = new_dist;
                    pq.emplace(new_dist, t);
                }
            }
        }
    }
};

int main() {
    int H, W, K;
    cin >> H >> W >> K;

    vector<vector<long long>> map(H, vector<long long>(W));
    for (int y = 0; y < H; ++y) {
        for (int x = 0; x < W; ++x) {
            cin >> map[y][x];
        }
    }

    long long ng = 1LL << 30;
    long long ok = 0;

    while (ng - ok > 1) {
        long long mid = (ng + ok) / 2;
        Di di(H * W);

        for (int y = 0; y < H; ++y) {
            for (int x = 0; x < W; ++x) {
                int current = y * W + x + 1;
                if (x > 0) di.connect(current - 1, current, map[y][x] < mid);
                if (y > 0) di.connect((y - 1) * W + x + 1, current, map[y][x] < mid);
                if (x < W - 1) di.connect(current + 1, current, map[y][x] < mid);
                if (y < H - 1) di.connect((y + 1) * W + x + 1, current, map[y][x] < mid);
            }
        }

        di.solve(1);

        if (di.distance[H * W] + (map[0][0] < mid) <= K) {
            ok = mid;
        } else {
            ng = mid;
        }
    }

    cout << min(ok, 1000000000LL) << endl;

    return 0;
}
0