結果

問題 No.20 砂漠のオアシス
ユーザー MisterMister
提出日時 2020-08-22 04:58:19
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 16 ms / 5,000 ms
コード長 1,591 bytes
コンパイル時間 1,136 ms
コンパイル使用メモリ 98,340 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-05 09:19:35
合計ジャッジ時間 2,210 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

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

template <class T>
std::vector<T> vec(int len, T elem) { return std::vector<T>(len, elem); }

template <class T>
using MaxHeap = std::priority_queue<T>;

const std::vector<std::pair<int, int>>
    dxys{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

void solve() {
    int n, m, ox, oy;
    std::cin >> n >> m >> ox >> oy;
    --ox, --oy;
    std::swap(ox, oy);

    auto xss = vec(n, vec(n, 0));
    for (auto& xs : xss) {
        for (auto& x : xs) std::cin >> x;
    }

    auto dp = vec(2, vec(n, vec(n, 0)));
    dp[0][0][0] = m;
    MaxHeap<std::tuple<int, int, int, int>> heap;
    heap.emplace(dp[0][0][0], 0, 0, 0);

    while (!heap.empty()) {
        auto [d, t, x, y] = heap.top();
        heap.pop();
        if (d < dp[t][x][y]) continue;

        if (t == 0 && x == ox && y == oy) {
            dp[1][x][y] = dp[0][x][y] * 2;
            heap.emplace(dp[1][x][y], 1, x, y);
        }

        for (auto [dx, dy] : dxys) {
            int nx = x + dx,
                ny = y + dy;
            if (nx < 0 || n <= nx ||
                ny < 0 || n <= ny ||
                dp[t][nx][ny] >= dp[t][x][y] - xss[nx][ny]) continue;

            dp[t][nx][ny] = dp[t][x][y] - xss[nx][ny];
            heap.emplace(dp[t][nx][ny], t, nx, ny);
        }
    }

    std::cout << (dp[0][n - 1][n - 1] == 0 && dp[1][n - 1][n - 1] == 0
                      ? "NO"
                      : "YES")
              << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0