結果

問題 No.20 砂漠のオアシス
ユーザー けーむけーむ
提出日時 2020-05-14 10:27:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 32 ms / 5,000 ms
コード長 3,073 bytes
コンパイル時間 1,979 ms
コンパイル使用メモリ 183,796 KB
実行使用メモリ 8,932 KB
最終ジャッジ日時 2023-10-13 02:31:43
合計ジャッジ時間 3,213 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,356 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 23 ms
7,988 KB
testcase_06 AC 30 ms
8,920 KB
testcase_07 AC 29 ms
8,916 KB
testcase_08 AC 32 ms
8,840 KB
testcase_09 AC 30 ms
8,932 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 2 ms
4,356 KB
testcase_13 AC 3 ms
4,352 KB
testcase_14 AC 3 ms
4,348 KB
testcase_15 AC 3 ms
4,348 KB
testcase_16 AC 6 ms
4,464 KB
testcase_17 AC 5 ms
4,348 KB
testcase_18 AC 6 ms
4,352 KB
testcase_19 AC 7 ms
4,384 KB
testcase_20 AC 2 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\n"

template<class T>
struct Dijkstra {
    const T inf = numeric_limits<T>::max() / 2 - 1;
    vector<T> dist;
    vector<int> path;
    
    Dijkstra() {}
    Dijkstra(int n) {
        graph.resize(n);
        dist.resize(n);
        prev.resize(n);
    }
    
    void add_edge(int from, int to, T cost) {
        graph[from].emplace_back(to, cost);
    }
    
    void get_distance(int from) {
        priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> pq;
        fill(dist.begin(), dist.end(), inf);
        fill(prev.begin(), prev.end(), -1);
        dist[from] = 0;
        pq.push(make_pair(0, from));
        while(!pq.empty()) {
            pair<T, int> p = pq.top(); pq.pop();
            if (dist[p.second] < p.first) continue;
            for (auto e : graph[p.second]) {
                if (dist[e.first] > (dist[p.second] + e.second)) {
                    dist[e.first] = dist[p.second] + e.second;
                    prev[e.first] = p.second;
                    pq.push(make_pair(dist[e.first], e.first));
                }
            }
        }
    }
    
    void get_path(int to) {
        path = vector<int>(0);
        for (; to != -1; to = prev[to]) {
            path.push_back(to);
        }
        reverse(path.begin(), path.end());
    }
    
private:
    vector<vector<pair<int, T>>> graph;
    vector<T> prev;
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    const ll d[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
    ll n, v, ox, oy;
    cin >> n >> v >> ox >> oy;
    vector<vector<ll>> l(n, vector<ll>(n));
    rep(i, n) rep(j, n) cin >> l[i][j];
    Dijkstra<ll> di(n * n);
    rep(y, n) {
        rep(x, n) {
            rep(i, 4) {
                ll nx = x + d[i][0];
                ll ny = y + d[i][1];
                if ((nx < 0) || (nx >= n) || (ny < 0) || (ny >= n)) continue;
                di.add_edge(y * n + x, ny * n + nx, l[ny][nx]);
            }
        }
    }
    di.get_distance(0);
    vector<ll> sd(n * n);
    rep(i, n * n) sd[i] = di.dist[i];
    if (sd[(n - 1) * n + (n - 1)] < v) {
        cout << "YES" << endl;
        return 0;
    }
    if ((ox == 0) && (oy == 0)) {
        cout << "NO" << endl;
        return 0;
    }
    ox--; oy--;
    di.get_distance(oy * n + ox);
    vector<ll> od(n * n);
    rep(i, n * n) od[i] = di.dist[i];
    v -= sd[oy * n + ox];
    v *= 2;
    v -= od[(n - 1) * n + (n - 1)];
    if (v > 0) {
        cout << "YES" << endl;
        return 0;
    }
    cout << "NO" << endl;
    return 0;
}
0