結果

問題 No.20 砂漠のオアシス
ユーザー serser
提出日時 2017-12-15 11:56:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 213 ms / 5,000 ms
コード長 1,961 bytes
コンパイル時間 2,050 ms
コンパイル使用メモリ 90,760 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-21 06:41:52
合計ジャッジ時間 2,884 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <vector>
#include <climits>
#include <tuple>
using namespace std;

int DIR_X[] = {-1,  1,  0,  0};
int DIR_Y[] = { 0,  0,  1, -1};

typedef vector<int> vi;
typedef vector<vector<int>> vvi;

void dijkstra(vvi& field, vvi& cost, int sx, int sy) {
    cost.assign(field.size(), vi(field[0].size(), INT_MAX));

    auto queue = priority_queue<tuple<int,int,int>,
                                vector<tuple<int, int, int>>,
                                greater<tuple<int,int,int>>>();
    queue.emplace(sx, sy, 0);
    cost[sx][sy] = 0;

    while(!queue.empty()) {
        int ix, iy, ic;
        tie(ix, iy, ic) = queue.top();
        queue.pop();

        if(ic <= cost[ix][iy]) {
            for(int i=0; i<4; i++) {
                int nx = ix + DIR_X[i];
                if(nx<0 || field.size()<=nx) continue;
                int ny = iy + DIR_Y[i];
                if(ny<0 || field[0].size()<=ny) continue;

                int c = ic + field[nx][ny];
                if(c<cost[nx][ny]) {
                    cost[nx][ny] = c;
                    queue.emplace(nx,ny,c);
                }
            }
        }

    }
}

int main() {

    int len, life, ox, oy;
    scanf("%d%d%d%d", &len, &life, &ox, &oy);
    ox--;
    oy--;

    vvi field = vvi(len, vi(len));
    for(int y=0; y<len; y++) {
        for(int x=0; x<len; x++) {
            scanf("%d", &field[x][y]);
        }
    }

    vvi cost;
    dijkstra(field, cost, 0, 0);
    if(cost[len-1][len-1] < life) {
        printf("YES");
        return 0;
    }

    if(ox>0 || oy>0) {
        int oasis_life = life - cost[ox][oy];
        if(oasis_life <= 0) {
            printf("NO");
            return 0;
        }

        dijkstra(field, cost, ox, oy);
        if(oasis_life*2 - cost[len-1][len-1] > 0) {
            printf("YES");
            return 0;
        }
    }

    printf("NO");

    return 0;
}
0