結果

問題 No.20 砂漠のオアシス
ユーザー PachicobuePachicobue
提出日時 2017-06-07 23:27:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 26 ms / 5,000 ms
コード長 4,027 bytes
コンパイル時間 1,793 ms
コンパイル使用メモリ 178,680 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-21 07:13:26
合計ジャッジ時間 2,578 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep1(i, n) for (ll i = 1; i <= (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)

#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define pii pair<int, int>
#define vi vector<int>

using namespace std;
template <class S, class T>
ostream& operator<<(ostream& o, const pair<S, T>& p)
{
    return o << "(" << p.first << "," << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& o, const vector<T>& vc)
{
    o << "sz = " << vc.size() << endl
      << "[";
    for (const T& v : vc)
        o << v << ",";
    o << "]";
    return o;
}
using ll = long long;
constexpr ll MOD = 1000000007;

struct Edge {
    Edge(int to, int cost) : to{to}, cost{cost} {}
    int to;
    int cost;
};

class Graph
{
public:
    Graph(int v) : m_v{v} { m_table.resize(v); }
    void addEdge(int to, int from, int cost)
    {
        m_table[to].pb(Edge{from, cost});
    }
    void Dijkstra(const std::size_t s, std::vector<int>& d) const
    {
        assert(s < m_v);
        assert(d.size() == m_v);
        using P = std::pair<int, std::size_t>;
        std::priority_queue<P, std::vector<P>, std::greater<P>> q;
        for (std::size_t i = 0; i < m_v; i++) {
            d[i] = INF;
        }
        d[s] = 0;
        q.push(std::make_pair(0, s));
        while (not q.empty()) {
            const P& p = q.top();
            const int cost = p.first;
            const std::size_t v = p.second;
            q.pop();
            if (d[v] < cost) {
                continue;
            }
            for (const auto& e : m_table[v]) {
                if (d[e.to] > d[v] + e.cost) {
                    d[e.to] = d[v] + e.cost;
                    q.push(std::make_pair(d[e.to], e.to));
                }
            }
        }
    }
    static constexpr int INF = numeric_limits<int>::max() / 100;

private:
    int m_v;
    vector<vector<Edge>> m_table;
};

int N;
inline int encode(int i, int j)
{
    return i * N + j;
}

inline pii decode(int n)
{
    return mp(n / N, n % N);
}

int main()
{
    int V, Ox, Oy;
    cin >> N >> V >> Ox >> Oy;
    Graph g{N * N};
    vector<vector<int>> level(N, vector<int>(N));
    for (int j = 0; j < N; j++) {
        for (int i = 0; i < N; i++) {
            cin >> level[i][j];
        }
    }

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            if (i > 0) {
                g.addEdge(encode(i - 1, j), encode(i, j), level[i][j]);
            }
            if (i < N - 1) {
                g.addEdge(encode(i + 1, j), encode(i, j), level[i][j]);
            }
            if (j > 0) {
                g.addEdge(encode(i, j - 1), encode(i, j), level[i][j]);
            }
            if (j < N - 1) {
                g.addEdge(encode(i, j + 1), encode(i, j), level[i][j]);
            }
        }
    }

    const int s = 0;
    const int t = N * N - 1;
    if (Ox == 0 and Oy == 0) {
        vector<int> d(N * N);
        g.Dijkstra(s, d);
        const int di = d[t];
        if (V <= di) {
            cout << "NO" << endl;
        } else {
            cout << "YES" << endl;
        }
    } else {
        Ox--;
        Oy--;
        const int oasis = encode(Ox, Oy);
        vector<int> d(N * N);
        g.Dijkstra(s, d);
        if (V > d[t]) {
            cout << "YES" << endl;
            return 0;
        }
        const int di1 = d[oasis];
        if (V <= di1) {
            cout << "NO" << endl;
        } else {
            V = (V - di1) * 2;
            g.Dijkstra(oasis, d);
            const int di2 = d[t];
            if (V <= di2) {
                cout << "NO" << endl;
            } else {
                cout << "YES" << endl;
            }
        }
    }

    return 0;
}
0