結果

問題 No.20 砂漠のオアシス
ユーザー LayCurseLayCurse
提出日時 2014-11-07 11:12:22
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 17 ms / 5,000 ms
コード長 2,914 bytes
コンパイル時間 1,685 ms
コンパイル使用メモリ 148,288 KB
実行使用メモリ 5,156 KB
最終ジャッジ日時 2023-08-03 07:19:57
合計ジャッジ時間 2,499 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 10 ms
4,864 KB
testcase_06 AC 17 ms
5,152 KB
testcase_07 AC 16 ms
5,156 KB
testcase_08 AC 16 ms
5,048 KB
testcase_09 AC 16 ms
5,000 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 3 ms
4,376 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 4 ms
4,380 KB
testcase_19 AC 4 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

#define mygc(c) (c)=getchar_unlocked()
#define mypc(c) putchar_unlocked(c)

void reader(int *x){int k,m=0;*x=0;for(;;){mygc(k);if(k=='-'){m=1;break;}if('0'<=k&&k<='9'){*x=k-'0';break;}}for(;;){mygc(k);if(k<'0'||k>'9')break;*x=(*x)*10+k-'0';}if(m)(*x)=-(*x);}
void reader(int *x, int *y){reader(x);reader(y);}
void reader(int *x, int *y, int *z){reader(x);reader(y);reader(z);}
void writer(const char c[]){int i;for(i=0;c[i]!='\0';i++)mypc(c[i]);}

template <class T>
struct DijkstraHeap {
  int *hp, *place, size; char *visited; T *val;

  void malloc(int N){hp=(int*)std::malloc(N*sizeof(int));place=(int*)std::malloc(N*sizeof(int));visited=(char*)std::malloc(N*sizeof(char));val=(T*)std::malloc(N*sizeof(T));}
  void free(){free(hp);free(place);free(visited);free(val);}
  void* malloc(int N, void *workMemory){hp=(int*)workMemory;place=(int*)(hp+N);visited=(char*)(place+N);val=(T*)(visited+N);workMemory=(void*)(val+N);return workMemory;}
  void init(int N){int i;size=0;rep(i,N)place[i]=-1,visited[i]=0;}
  void up(int n){int m;while(n){m=(n-1)/2;if(val[hp[m]]<=val[hp[n]])break;swap(hp[m],hp[n]);swap(place[hp[m]],place[hp[n]]);n=m;}}
  void down(int n){int m;for(;;){m=2*n+1;if(m>=size)break;if(m+1<size&&val[hp[m]]>val[hp[m+1]])m++;if(val[hp[m]]>=val[hp[n]]) break;swap(hp[m],hp[n]);swap(place[hp[m]],place[hp[n]]);n=m;}}
  void change(int n, T v){if(visited[n]||(place[n]>=0&&val[n]<=v))return;val[n]=v;if(place[n]==-1)place[n]=size,hp[size++]=n,up(place[n]);else up(place[n]);}
  int pop(void){int res=hp[0];place[res]=-1;size--;if(size)hp[0]=hp[size],place[hp[0]]=0,down(0);visited[res]=1;return res;}
};

int N, V, Ox, Oy, mp[555][555];
int d1[555][555], d2[555][555];

int main(){
  int i, j, k;
  int di[4]={-1,1,0,0}, dj[4]={0,0,-1,1}, d, ni, nj;
  DijkstraHeap<int> hp;

  reader(&N,&V);
  reader(&Oy,&Ox); Ox--; Oy--;
  rep(i,N) rep(j,N) reader(mp[i]+j);

  hp.malloc(N*N);

  hp.init(N*N);
  hp.change(0, 0);
  while(hp.size){
    k = hp.pop();
    i = k / N; j = k % N;
    rep(d,4){
      ni = i + di[d];
      nj = j + dj[d];
      if(ni < 0 || nj < 0 || ni >= N || nj >= N) continue;
      hp.change(ni*N+nj, hp.val[k]+mp[ni][nj]);
    }
  }
  rep(i,N) rep(j,N) d1[i][j] = hp.val[i*N+j];

  if(d1[N-1][N-1] < V){ writer("YES\n"); return 0; }
  if(Ox < 0){ writer("NO\n"); return 0; }

  hp.init(N*N);
  hp.change(Ox*N+Oy, 0);
  while(hp.size){
    k = hp.pop();
    i = k / N; j = k % N;
    rep(d,4){
      ni = i + di[d];
      nj = j + dj[d];
      if(ni < 0 || nj < 0 || ni >= N || nj >= N) continue;
      hp.change(ni*N+nj, hp.val[k]+mp[ni][nj]);
    }
  }
  rep(i,N) rep(j,N) d2[i][j] = hp.val[i*N+j];

  if((V-d1[Ox][Oy])*2-d2[N-1][N-1] <= 0) writer("NO\n"); else writer("YES\n");

  return 0;
}
0