結果

問題 No.20 砂漠のオアシス
ユーザー rodearodea
提出日時 2019-10-06 23:31:20
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 25 ms / 5,000 ms
コード長 2,751 bytes
コンパイル時間 1,486 ms
コンパイル使用メモリ 121,460 KB
実行使用メモリ 8,064 KB
最終ジャッジ日時 2024-04-20 01:12:33
合計ジャッジ時間 2,191 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#pragma GCC optimize("O3")
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;

using ll = long long;
using P = pair<int, int>;
using T = tuple<int, int, int>;

template <class T> inline T chmax(T &a, const T b) {return a = (a < b) ? b : a;}
template <class T> inline T chmin(T &a, const T b) {return a = (a > b) ? b : a;}

constexpr int MOD = 1e9 + 7;
constexpr int inf = 1e9;
constexpr long long INF = 1e18;
constexpr double pi = acos(-1);
constexpr double EPS = 1e-10;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

int n;

vector<int> dijkstra(int s, vector<vector<P>> &G){
    vector<int> dist(n*n, inf);
    priority_queue<P, vector<P>, greater<P>> que;
    dist[s] = 0;
    que.emplace(0, s);

    while(que.size()){
        int ccost, cv;
        tie(ccost, cv) = que.top(); que.pop();

        if(dist[cv] < ccost) continue;

        for(auto nxt : G[cv]){
            int nv, ncost;
            tie(nv, ncost) = nxt;
            
            if(dist[cv] + ncost < dist[nv]){
                dist[nv] = dist[cv] + ncost;
                que.emplace(dist[nv], nv);
            }
        }
    }

    return dist;
}

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

    int v, ox, oy; cin>>n>>v>>ox>>oy;
    ox--, oy--;
    int l[n][n];
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++) cin>>l[i][j];
    }

    int grid2graph[n][n];
    int cnt = 0;
    int o = -1;
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            if(i == oy && j == ox) o = cnt;
            grid2graph[i][j] = cnt++;
        }
    }

    vector<vector<P>> G(n*n);
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            int cy = i, cx = j;
            for(int k=0; k<4; k++){
                int ny = cy + dy[k], nx = cx + dx[k];
                if(!(0 <= ny && ny < n && 0 <= nx && nx < n)) continue;

                int cv = grid2graph[cy][cx], nv = grid2graph[ny][nx];
                G[cv].emplace_back(nv, l[ny][nx]);
                G[nv].emplace_back(cv, l[cy][cx]);
            }
        }
    }

    auto sdist = dijkstra(0, G);

    bool valid = false;
    if(sdist[n*n-1] < v) valid = true;
    if(o != -1){
        auto odist = dijkstra(o, G);
        if(odist[n*n-1] < 2 * (v - sdist[o])) valid = true;
    }

    cout << (valid ? "YES" : "NO") << endl;

    return 0;
}
0