結果

問題 No.20 砂漠のオアシス
ユーザー sarashinsarashin
提出日時 2019-07-01 14:53:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,860 bytes
コンパイル時間 1,647 ms
コンパイル使用メモリ 179,540 KB
実行使用メモリ 7,808 KB
最終ジャッジ日時 2023-09-23 01:27:37
合計ジャッジ時間 2,749 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
#define ll long long
#define INF 1e9
#define LINF 1e18
#define rep(i,n) for(int i=0;i<n;++i)
using namespace std;
typedef pair<ll,ll> P;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

typedef struct Edge {
    ll to,cost;
} Edge;

vector<vector<Edge>> g;
int L[222][222];

void make_graph(int n, int m) {
    rep(i,n) {
        rep(j,m) {
            rep(k,4) {
                int now = i*n+j;
                int nx = i+dx[k]; int ny = j+dy[k];
                if (-1 < nx && nx < n && -1 < ny && ny < m) {
                    // printf("now = %d, nx = %d, ny = %d\n", now,nx,ny);
                    int cost = L[nx][ny];
                    int to = nx*n+ny;
                    g[now].push_back(Edge{to,cost});
                } 
            }
        }
    }
}

void dijkstra(vector<ll> &dp, int s) {
    priority_queue<P, vector<P>, greater<P>> pq;
    pq.push({0,s});
    dp[s] = 0;

    while (!pq.empty()) {
        P p = pq.top(); pq.pop();
        int v = p.second;
        for (auto &&e: g[v]) {
            if (dp[e.to] > dp[v]+e.cost) {
                dp[e.to] = dp[v]+e.cost;
                pq.push({dp[e.to], e.to});
            }
        }
    }
}

int main() {
    int n,v,ox,oy;
    cin >> n >> v >> ox >> oy;
    --ox, --oy;
    g.resize(n*n);
    rep(i,n) rep(j,n) cin >> L[i][j];
    make_graph(n,n);

    vector<ll> dp(n*n, INF);
    dijkstra(dp,0);
    int goal = n*n-1;
    if (v-dp[goal] > 0) {
        cout << "YES" << endl;
        return 0;
    }

    if (ox == -1 && oy == -1) {
        cout << "NO" << endl;
        return 0;
    }
    
    int oasis = ox*n+oy;
    int oasis_v = v-dp[oasis];
    oasis_v *= 2;
    dp.assign(n*n,INF);
    dijkstra(dp,oasis);
    
    if (oasis_v-dp[goal] > 0) {
        cout << "YES" << endl;
        return 0;
    }

    cout << "NO" << endl;
    return 0;
}
0