結果

問題 No.20 砂漠のオアシス
ユーザー ser00ser00
提出日時 2017-12-11 20:51:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,920 bytes
コンパイル時間 876 ms
コンパイル使用メモリ 81,112 KB
実行使用メモリ 8,416 KB
最終ジャッジ日時 2023-08-20 07:58:19
合計ジャッジ時間 7,877 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

struct Item {
    int x;
    int y;
    int cost;

    bool operator<(const Item& rhs) const {
        return cost < rhs.cost;
    }
};

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

typedef vector<vector<int> > cost_matrix_t;

cost_matrix_t dijkstra(int field[][200], int sx, int sy, int len_x, int len_y) {
    auto queue = priority_queue<Item>();

    auto costmatrix = vector<vector<int>>(len_x, vector<int>(len_y, INT_MAX));

    queue.push(Item{sx,sy,0});
    costmatrix[sx][sy] = 0;

    while(!queue.empty()) {
        Item item = queue.top();
        queue.pop();

        if(item.cost <= costmatrix[item.x][item.y]) {
            for(int i=0; i<4; i++) {
                int nx = item.x + DIR_X[i];
                if(nx<0 || len_x<=nx) continue;
                int ny = item.y + DIR_Y[i];
                if(ny<0 || len_y<=ny) continue;

                int c = item.cost + field[nx][ny];
                if(c<costmatrix[nx][ny]) {
                    costmatrix[nx][ny] = c;
                    queue.push(Item{nx,ny,c});
                }
            }
        }

    }

    return costmatrix;
}

int main() {

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

    int field[200][200];
    for(int i=0; i<len; i++) {
        for(int j=0; j<len; j++) {
            scanf("%d", &field[i][j]);
        }
    }

    cost_matrix_t c = dijkstra(field, 0, 0, len, len);
    if(c[len-1][len-1] < life) {
        printf("YES");

    } else if(ox>0 || oy>0) {
        cost_matrix_t c2 = dijkstra(field, ox, oy, len, len);
        if((life - c[ox][oy])*2 - c2[len-1][len-1] > 0) {
            printf("YES");
        } else {
            printf("NO");
        }
    } else {
        printf("NO");
    }

    return 0;
}
0