結果

問題 No.20 砂漠のオアシス
ユーザー fantasiabaeticafantasiabaetica
提出日時 2018-08-11 00:07:17
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 18 ms / 5,000 ms
コード長 2,982 bytes
コンパイル時間 599 ms
コンパイル使用メモリ 67,304 KB
実行使用メモリ 4,356 KB
最終ジャッジ日時 2023-10-24 13:39:58
合計ジャッジ時間 1,477 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <queue>
#define FOR(i,a,b) for(int i=(a); i<(b); i++)
#define P pair<int, int>
#define INF 1<<27
using namespace std;

int N, life, ox, oy;
int map[250][250];
int cost[250][250];
int cost_oasis[250][250];

void dijkstra(){
    int dx[4] = {1, -1, 0, 0};
    int dy[4] = {0, 0, 1, -1};
    fill(cost[0], cost[200], INF);
    cost[0][0] = 0;
    // (最短距離, x + 300y)
    priority_queue<P, vector<P>, greater<P> > q;
    q.push(P(0, 0));
    while (!(q.empty())){
        P top = q.top();
        q.pop();
        // 四方向を見る。更新できる場所があればpriority queueに追加する。
        int x, y;
        x = top.second % 300;
        y = top.second / 300;
        // 最短距離が更新されていた場合
        if (cost[x][y] < top.first) continue;
        FOR(i, 0, 4){
            int tmpx, tmpy;
            tmpx = x + dx[i];
            tmpy = y + dy[i];
            if (tmpx < 0 || tmpx >= N || tmpy < 0 || tmpy >= N) continue;
            if (cost[x][y] + map[tmpx][tmpy] < cost[tmpx][tmpy]){
                cost[tmpx][tmpy] = cost[x][y] + map[tmpx][tmpy];
                q.push(P(cost[tmpx][tmpy], tmpx + tmpy * 300));
            }
        }
    }
}

void dijkstra_oasis(){
    int dx[4] = {1, -1, 0, 0};
    int dy[4] = {0, 0, 1, -1};
    fill(cost_oasis[0], cost_oasis[200], INF);
    cost_oasis[ox][oy] = 0;
    // (最短距離, x + 300y)
    priority_queue<P, vector<P>, greater<P> > q;
    q.push(P(0, ox + oy * 300));
    while (!(q.empty())){
        P top = q.top();
        q.pop();
        // 四方向を見る。更新できる場所があればpriority queueに追加する。
        int x, y;
        x = top.second % 300;
        y = top.second / 300;
        // 最短距離が更新されていた場合
        if (cost_oasis[x][y] < top.first) continue;
        FOR(i, 0, 4){
            int tmpx, tmpy;
            tmpx = x + dx[i];
            tmpy = y + dy[i];
            if (tmpx < 0 || tmpx >= N || tmpy < 0 || tmpy >= N) continue;
            if (cost_oasis[x][y] + map[tmpx][tmpy] < cost_oasis[tmpx][tmpy]){
                cost_oasis[tmpx][tmpy] = cost_oasis[x][y] + map[tmpx][tmpy];
                q.push(P(cost_oasis[tmpx][tmpy], tmpx + tmpy * 300));
            }
        }
    }
}

bool possible(){
    if (cost[N-1][N-1] < life) return true;
    if (ox == 0 && oy == 0){
        return false;
    }
    if (cost[ox][oy] >= life) return false;
    int new_life = (life - cost[ox][oy]) * 2;
    if (cost_oasis[N-1][N-1] < new_life) return true;
    return false;
}

int main(){
    cin >> N >> life >> oy >> ox;
    // 0オリジンにする
    if (!(ox == 0 && oy == 0)){
        ox -= 1;
        oy -= 1;
    } 
    FOR(i, 0, N){
        FOR(j, 0, N){
            cin >> map[i][j];
        }
    }
    dijkstra();
    if (!(ox == 0 && oy == 0)){
        dijkstra_oasis();
    }

    if (possible()) cout << "YES" << endl;
    else cout << "NO" << endl;

    return 0;
}
0