結果

問題 No.20 砂漠のオアシス
ユーザー fumofumofunifumofumofuni
提出日時 2020-09-17 20:21:52
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 22 ms / 5,000 ms
コード長 2,804 bytes
コンパイル時間 1,845 ms
コンパイル使用メモリ 179,600 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-04 07:30:40
合計ジャッジ時間 2,917 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={1,0,-1,0,1,1,-1,-1};
const ll dx[8]={0,-1,0,1,1,-1,1,-1};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
int main(){
    ll n,v,sx,sy;cin >> n >> v >> sy >> sx;
    vvl g(n,vl(n));
    rep(i,n)rep(j,n)cin >> g[i][j];
    priority_queue<pair<ll,pl>,vector<pair<ll,pl>>,greater<pair<ll,pl>>> que;
    que.push(make_pair(0,make_pair(0,0)));
    vvl dist(n,vl(n,inf/10));
    dist[0][0]=0;
    while(!que.empty()){
        pl v=que.top().se;
        ll edge=que.top().fi;
        que.pop();
        if(dist[v.fi][v.se]<edge)continue;
        rep(i,4){
            ll ny=v.fi+dy[i],nx=v.se+dx[i];
            if(ny<0||nx<0||ny>=n||nx>=n)continue;
            ll ed=g[ny][nx];
            //if(grid[ny][nx]=='#')ed=mid;
            if(chmin(dist[ny][nx],dist[v.fi][v.se]+ed)){
                //prev[ny][nx]=v; //restore
                que.push(make_pair(dist[ny][nx],make_pair(ny,nx)));
            }
        }
    }
    /*rep(i,n){
        rep(j,n)cout << dist[i][j] <<" ";cout << endl;
    }*/
    if(dist[n-1][n-1]<v)cout << "YES" <<endl,exit(0);
    if(dist[n-1][n-1]>=v&&sx==0)cout << "NO" <<endl,exit(0);
    if(dist[sx-1][sy-1]>=v)cout << "NO" <<endl,exit(0);
    v=(v-dist[sx-1][sy-1])*2;
    que.push(make_pair(0,make_pair(sx-1,sy-1)));
    dist=vvl(n,vl(n,INF/10));
    dist[sx-1][sy-1]=0;
    while(!que.empty()){
        pl v=que.top().se;
        ll edge=que.top().fi;
        que.pop();
        if(dist[v.fi][v.se]<edge)continue;
        rep(i,4){
            ll ny=v.fi+dy[i],nx=v.se+dx[i];
            if(ny<0||nx<0||ny>=n||nx>=n)continue;
            ll ed=g[ny][nx];
            //if(grid[ny][nx]=='#')ed=mid;
            if(chmin(dist[ny][nx],dist[v.fi][v.se]+ed)){
                //prev[ny][nx]=v; //restore
                que.push(make_pair(dist[ny][nx],make_pair(ny,nx)));
            }
        }
    }
    if(dist[n-1][n-1]<v)cout << "YES" <<endl;
    else cout << "NO" <<endl;
}
0