結果

問題 No.20 砂漠のオアシス
ユーザー dnishdnish
提出日時 2018-06-23 16:18:57
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 21 ms / 5,000 ms
コード長 3,061 bytes
コンパイル時間 1,700 ms
コンパイル使用メモリ 173,128 KB
実行使用メモリ 4,936 KB
最終ジャッジ日時 2023-09-13 08:30:44
合計ジャッジ時間 2,812 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include "bits/stdc++.h"
#define REP(i,n,N) for(ll i=(n); i<(N); i++)
#define RREP(i,n,N) for(ll i=(N-1); i>=n; i--)
#define CK(n,a,b) ((a)<=(n)&&(n)<(b))
#define ALL(v) (v).begin(),(v).end()
#define p(s) cout<<(s)<<endl
#define p2(a,b) cout<<(a)<<" "<<(b)<<endl
#define v2(T) vector<vector<T>>
typedef long long ll;
using namespace std;
const ll inf=1e18;

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

ll N, V, OY, OX;
ll L[210][210];

ll d1[210][210];//(y,x)に行くのにかかるコストの最小
void dijkstra1(){
    priority_queue< pair<ll, pair<ll,ll> > > PQ;    //(y,x)に行くのにかかるコスト*(-1)

    REP(i,0,N){
        REP(j,0,N){
            d1[i][j] = inf;
        }
    }

    d1[0][0] = 0;
    PQ.push({0, {0,0}});

    while(!PQ.empty()){
        ll curCost = -PQ.top().first;
        ll y = PQ.top().second.first;
        ll x = PQ.top().second.second;
        PQ.pop();

        if(d1[y][x] < curCost){
            continue;
        }
        d1[y][x] = curCost;

        REP(k,0,4){
            ll ny = y + dy[k];
            ll nx = x + dx[k];

            if(!CK(ny,0,N) || !CK(nx,0,N)) continue;

            ll addCost = L[ny][nx];

            if(d1[ny][nx] > curCost + addCost){
                d1[ny][nx] = curCost + addCost;
                PQ.push({-d1[ny][nx], {ny, nx}});
            }
        }

    }
}

ll d2[210][210];    //オアシスからのコスト
void dijkstra2(){
    priority_queue< pair<ll, pair<ll,ll> > > PQ;    //(y,x)に行くのにかかるコスト*(-1)

    REP(i,0,N){
        REP(j,0,N){
            d2[i][j] = inf;
        }
    }

    d2[OY-1][OX-1] = 0;
    PQ.push({0, {OY-1,OX-1}});

    while(!PQ.empty()){
        ll curCost = -PQ.top().first;
        ll y = PQ.top().second.first;
        ll x = PQ.top().second.second;
        PQ.pop();

        if(d2[y][x] < curCost){
            continue;
        }
        d2[y][x] = curCost;

        REP(k,0,4){
            ll ny = y + dy[k];
            ll nx = x + dx[k];

            if(!CK(ny,0,N) || !CK(nx,0,N)) continue;

            ll addCost = L[ny][nx];

            if(d2[ny][nx] > curCost + addCost){
                d2[ny][nx] = curCost + addCost;
                PQ.push({-d2[ny][nx], {ny, nx}});
            }
        }

    }
}

int main(){
    while(cin>>N>>V>>OX>>OY){
        REP(i,0,N){
            REP(j,0,N){
                cin>>L[i][j];
            }
        }

        dijkstra1();
        //cout<<"d>> "<<d1[OY-1][OX-1]<<endl;


        //オアシスに行かずにゴールできる.
        if(d1[N-1][N-1] < V){
            cout<<"YES"<<endl;
            continue;
        }

        if((OY == 0 && OX == 0)){
            cout<<"NO"<<endl;
            continue;
        }else if(d1[OY-1][OX-1] >= V){
            cout<<"NO"<<endl;
            continue;
        }else{
            //cout<<"d2"<<endl;
            dijkstra2();

            if(d2[N-1][N-1] < (V-d1[OY-1][OX-1])*2){
                cout<<"YES"<<endl;
            }else{
                cout<<"NO"<<endl;
            }

        }
    }

    return 0;
}
0