結果

問題 No.20 砂漠のオアシス
ユーザー kyunakyuna
提出日時 2019-10-20 15:41:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 34 ms / 5,000 ms
コード長 1,771 bytes
コンパイル時間 880 ms
コンパイル使用メモリ 88,500 KB
実行使用メモリ 7,912 KB
最終ジャッジ日時 2023-09-15 14:37:53
合計ジャッジ時間 2,067 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <tuple>
using namespace std;
const long long INF = 1LL << 60;    // 1.15x10^18
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using edge = pair<int, long long>;
using Graph = vector<vector<edge>>;

vector<long long> dijkstra(const Graph &g, int s) {
    vector<long long> dist(g.size(), INF);
    using Pi = pair<long long, int>;
    priority_queue<Pi, vector<Pi>, greater<Pi>> que;
    dist[s] = 0; que.emplace(dist[s], s);
    while (!que.empty()) {
        long long cost; int u; tie(cost, u) = que.top(); que.pop();
        if (dist[u] < cost) continue;
        for (auto &e: g[u]) {
            int v; long long nc; tie(v, nc) = e;
            if (chmin(dist[v], dist[u] + nc)) que.emplace(dist[v], v);
        }
    }
    return dist;
}

int main() {
    int n, v, oi, oj; cin >> n >> v >> oj >> oi;
    Graph g(n * n);
    auto idx = [&](int i, int j) { return i * n + j; };
    auto out_field = [&](int i, int j) {
        return i < 0 || i >= n || j < 0 || j >= n;
    };
    int dij[] = {0, 1, 0, -1, 0};
    for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) {
        int cost; cin >> cost;
        for (int k = 0; k < 4; k++) {
            int ni = i + dij[k], nj = j + dij[k + 1];
            if (out_field(ni, nj)) continue;
            g[idx(ni, nj)].emplace_back(idx(i, j), cost);
        }
    }
    auto dist_s = dijkstra(g, idx(0, 0));
    bool ok = dist_s[idx(n - 1, n - 1)] < v;
    if (oi != 0 && oj != 0) {
        oi--, oj--;
        auto dist_o = dijkstra(g, idx(oi, oj));
        ok |= dist_o[idx(n - 1, n - 1)] < 2 * (v - dist_s[idx(oi, oj)]);
    }
    cout << (ok ? "YES" : "NO") << endl;
    return 0;
}
0