結果

問題 No.20 砂漠のオアシス
ユーザー commy
提出日時 2018-08-12 22:45:21
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 28 ms / 5,000 ms
コード長 1,967 bytes
コンパイル時間 1,056 ms
コンパイル使用メモリ 92,428 KB
実行使用メモリ 8,576 KB
最終ジャッジ日時 2024-09-24 07:37:19
合計ジャッジ時間 2,274 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
#define dump(val) cerr << __LINE__ << ":\t" << #val << " = " << (val) << endl

using namespace std;

typedef long long int lli;

template<typename T>
vector<T> make_v(size_t a, T b) {
    return vector<T>(a, b);
}

template<typename... Ts>
auto make_v(size_t a, Ts... ts) {
    return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

struct Edge {
    int x, y, hp, o;
    Edge(int _x, int _y, int h, int _o)
        : x(_x), y(_y), hp(h), o(_o) {}
    Edge() {}
    bool operator<(const Edge &e) const {
        return hp < e.hp;
    }
};

int main() {
    int N, V, Ox, Oy;
    cin >> N >> V >> Ox >> Oy;
    auto L = make_v(N + 2, N + 2, -1);
    REP(i, 1, N + 1) {
        REP(j, 1, N + 1) {
            cin >> L[i][j];
        }
    }
    priority_queue<Edge, vector<Edge>> pq;
    pq.emplace(1, 1, V, 0);
    auto used = make_v(N + 2, N + 2, 2, false);
    auto Cost = make_v(N + 2, N + 2, 2, 0);
    const int dx[4] = {0, 1, 0, -1};
    const int dy[4] = {1, 0, -1, 0};
    while (pq.size()) {
        auto E = pq.top();
        pq.pop();
        if (used[E.y][E.x][E.o]) continue;
        used[E.y][E.x][E.o] = true;
        if (Cost[E.y][E.x][E.o] > E.hp) continue;
        Cost[E.y][E.x][E.o] = E.hp;
        REP(k, 0, 4) {
            int nx = E.x + dx[k];
            int ny = E.y + dy[k];
            int no = E.o;
            if (L[ny][nx] == -1) continue;
            int nc = E.hp - L[ny][nx];
            if (ny == Oy && nx == Ox && !no) {
                nc *= 2;
                no = 1;
            }
            if (nc <= 0) continue;
            if (Cost[ny][nx][no] < nc) {
                Cost[ny][nx][no] = nc;
                pq.emplace(nx, ny, nc, no);
            }
        }
    }
    cout << (((Cost[N][N][0] > 0) || (Cost[N][N][1] > 0)) ? "YES" : "NO") << endl;
    return 0;
}
0