結果

問題 No.20 砂漠のオアシス
ユーザー neterukunneterukun
提出日時 2022-04-12 20:59:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,125 bytes
コンパイル時間 846 ms
コンパイル使用メモリ 85,800 KB
実行使用メモリ 4,568 KB
最終ジャッジ日時 2023-08-23 06:18:11
合計ジャッジ時間 2,864 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 AC 1 ms
4,376 KB
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 AC 21 ms
4,444 KB
testcase_06 AC 22 ms
4,376 KB
testcase_07 WA -
testcase_08 RE -
testcase_09 AC 22 ms
4,380 KB
testcase_10 WA -
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 4 ms
4,376 KB
testcase_18 WA -
testcase_19 AC 6 ms
4,376 KB
testcase_20 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <tuple>
#include <vector>
#include <queue>


const int di[] = {0, 0, 1, -1};
const int dj[] = {1, -1, 0, 0};


std::vector<std::vector<long long>> dijkstra(const std::vector<std::vector<long long>> &grid, int si, int sj) {
    using P = std::tuple<long long, int, int>;
    int h = grid.size();
    int w = grid[0].size();

    std::vector<std::vector<long long>> distance(h, std::vector<long long>(w, 1LL << 60));
    distance[si][sj] = 0LL;

    std::priority_queue<P, std::vector<P>, std::greater<P>> pq;
    pq.emplace(0LL, si, sj);

    while (not pq.empty()) {
        auto p = pq.top();
        pq.pop();

        int& i = std::get<1>(p);
        int& j = std::get<2>(p);
        if (distance[i][j] < std::get<0>(p)) continue;

        for (int k = 0; k < 4; k++) {
            int nxt_i = i + di[k];
            int nxt_j = j + dj[k];
            if (nxt_i < 0 || h <= nxt_i) continue;
            if (nxt_j < 0 || w <= nxt_j) continue;
            if (distance[nxt_i][nxt_j] > distance[i][j] + grid[nxt_i][nxt_j]) {
                distance[nxt_i][nxt_j] = distance[i][j] + grid[nxt_i][nxt_j];
                pq.emplace(distance[nxt_i][nxt_j], nxt_i, nxt_j);
            }
        }
    }
    return distance;
}


int main() {
    long long v;
    int n, oj, oi;
    std::cin >> n >> v >> oj >> oi;
    oj -= 1;
    oi -= 1;
    std::vector<std::vector<long long>> l(n, std::vector<long long>(n, 0));
    for (auto& inner : l) {
        for (auto& val : inner) {
            std::cin >> val;
        }
    }

    auto distance = dijkstra(l, 0, 0);

    if (v - distance[n - 1][n - 1] > 0) {
        std::cout << "YES" << std::endl;
        return 1;
    }

    if (oi == -1 && oj == -1) {
        std::cout << "NO" << std::endl;
        return 1;
    }

    if (v - distance[oi][oj] <= 0) {
        std::cout << "NO" << std::endl;
        return 1;
    }

    v -= distance[oi][oj];
    v *= 2;
    auto mid_distance = dijkstra(l, oi, oj);

    if (v - distance[n - 1][n - 1] > 0) {
        std::cout << "YES" << std::endl;
    } else {
        std::cout << "NO" << std::endl;
    }
}

0