結果

問題 No.1638 Robot Maze
ユーザー 👑Zack Ni👑Zack Ni
提出日時 2021-08-06 22:02:33
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 11,278 bytes
コンパイル時間 2,101 ms
コンパイル使用メモリ 182,236 KB
実行使用メモリ 7,828 KB
最終ジャッジ日時 2023-10-17 03:25:03
合計ジャッジ時間 3,529 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,744 KB
testcase_01 AC 2 ms
7,744 KB
testcase_02 AC 2 ms
7,744 KB
testcase_03 AC 3 ms
7,828 KB
testcase_04 AC 3 ms
7,744 KB
testcase_05 AC 2 ms
7,744 KB
testcase_06 AC 2 ms
7,744 KB
testcase_07 AC 2 ms
7,744 KB
testcase_08 AC 2 ms
7,744 KB
testcase_09 AC 2 ms
7,744 KB
testcase_10 AC 2 ms
7,744 KB
testcase_11 AC 2 ms
7,744 KB
testcase_12 AC 2 ms
7,744 KB
testcase_13 AC 2 ms
7,744 KB
testcase_14 AC 3 ms
7,828 KB
testcase_15 AC 3 ms
7,828 KB
testcase_16 AC 3 ms
7,828 KB
testcase_17 AC 3 ms
7,828 KB
testcase_18 AC 2 ms
7,828 KB
testcase_19 AC 3 ms
7,828 KB
testcase_20 AC 3 ms
7,828 KB
testcase_21 AC 3 ms
7,828 KB
testcase_22 AC 3 ms
7,828 KB
testcase_23 AC 2 ms
7,828 KB
testcase_24 AC 2 ms
7,828 KB
testcase_25 AC 3 ms
7,828 KB
testcase_26 AC 3 ms
7,828 KB
testcase_27 AC 3 ms
7,828 KB
testcase_28 AC 3 ms
7,828 KB
testcase_29 AC 3 ms
7,828 KB
testcase_30 AC 2 ms
7,744 KB
testcase_31 AC 2 ms
7,744 KB
testcase_32 AC 4 ms
7,828 KB
testcase_33 AC 4 ms
7,828 KB
testcase_34 AC 4 ms
7,828 KB
testcase_35 AC 4 ms
7,828 KB
testcase_36 AC 4 ms
7,828 KB
testcase_37 AC 4 ms
7,828 KB
testcase_38 AC 4 ms
7,828 KB
testcase_39 AC 4 ms
7,828 KB
testcase_40 AC 4 ms
7,828 KB
testcase_41 AC 4 ms
7,828 KB
testcase_42 AC 4 ms
7,828 KB
testcase_43 AC 5 ms
7,828 KB
testcase_44 AC 4 ms
7,828 KB
testcase_45 AC 4 ms
7,828 KB
testcase_46 AC 4 ms
7,828 KB
testcase_47 AC 4 ms
7,828 KB
testcase_48 AC 4 ms
7,828 KB
testcase_49 AC 4 ms
7,828 KB
testcase_50 AC 4 ms
7,828 KB
testcase_51 AC 4 ms
7,828 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void*wmem;
char memarr[96000000];
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
  static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
template<class T> inline void walloc1d(T **arr, int x1, int x2, void **mem = &wmem){
  walloc1d(arr, x2-x1, mem);
  (*arr) -= x1;
}
inline int my_getchar(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void rd(long long &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void rd(char &c){
  int i;
  for(;;){
    i = my_getchar();
    if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF){
      break;
    }
  }
  c = i;
}
inline int rd(char c[]){
  int i;
  int sz = 0;
  for(;;){
    i = my_getchar();
    if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF){
      break;
    }
  }
  c[sz++] = i;
  for(;;){
    i = my_getchar();
    if(i==' '||i=='\n'||i=='\r'||i=='\t'||i==EOF){
      break;
    }
    c[sz++] = i;
  }
  c[sz]='\0';
  return sz;
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar(a);
}
inline void wt_L(const char c[]){
  int i=0;
  for(i=0;c[i]!='\0';i++){
    my_putchar(c[i]);
  }
}
template<class S> inline void arrInsert(const int k, int &sz, S a[], const S aval){
  int i;
  sz++;
  for(i=sz-1;i>k;i--){
    a[i] = a[i-1];
  }
  a[k] = aval;
}
template<class S, class T> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval){
  int i;
  sz++;
  for(i=sz-1;i>k;i--){
    a[i] = a[i-1];
  }
  for(i=sz-1;i>k;i--){
    b[i] = b[i-1];
  }
  a[k] = aval;
  b[k] = bval;
}
template<class S, class T, class U> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval){
  int i;
  sz++;
  for(i=sz-1;i>k;i--){
    a[i] = a[i-1];
  }
  for(i=sz-1;i>k;i--){
    b[i] = b[i-1];
  }
  for(i=sz-1;i>k;i--){
    c[i] = c[i-1];
  }
  a[k] = aval;
  b[k] = bval;
  c[k] = cval;
}
template<class S, class T, class U, class V> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval, V d[], const V dval){
  int i;
  sz++;
  for(i=sz-1;i>k;i--){
    a[i] = a[i-1];
  }
  for(i=sz-1;i>k;i--){
    b[i] = b[i-1];
  }
  for(i=sz-1;i>k;i--){
    c[i] = c[i-1];
  }
  for(i=sz-1;i>k;i--){
    d[i] = d[i-1];
  }
  a[k] = aval;
  b[k] = bval;
  c[k] = cval;
  d[k] = dval;
}
template <class T> struct DijkstraHeap{
  int*hp;
  int*place;
  int 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(){
    std::free(hp);
    std::free(place);
    std::free(visited);
    std::free(val);
  }
  void walloc(int N, void **mem=&wmem){
    walloc1d(&hp, N, mem);
    walloc1d(&place, N, mem);
    walloc1d(&visited, N, mem);
    walloc1d(&val, N, mem);
  }
  void init(int N){
    int i;
    size = 0;
    for(i=(0);i<(N);i++){
      place[i]=-1;
    }
    for(i=(0);i<(N);i++){
      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;
  }
}
;
struct graph{
  int N;
  int*es;
  int**edge;
  void setDirectEdge(int N__, int M, int A[], int B[], void **mem = &wmem){
    int i;
    N = N__;
    walloc1d(&es, N, mem);
    walloc1d(&edge, N, mem);
    walloc1d(&edge[0], M, mem);
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      es[A[i]]++;
    }
    for(i=(0);i<(N);i++){
      walloc1d(&edge[i], es[i], mem);
    }
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      edge[A[i]][es[A[i]]++] = B[i];
    }
  }
  void getDist(int root, int res[], void *mem = wmem){
    int i;
    int j;
    int k;
    int*q;
    int s;
    int z;
    walloc1d(&q, N, &mem);
    for(i=(0);i<(N);i++){
      res[i]=-1;
    }
    res[root]=0;
    s=0;
    z=1;
    q[0]=root;
    while(z){
      i=q[s++];
      z--;
      for(j=(0);j<(es[i]);j++){
        k=edge[i][j];
        if(res[k]>=0){
          continue;
        }
        res[k]=res[i]+1;
        q[s+z++]=k;
      }
    }
  }
  int getDist(int a, int b, void *mem = wmem){
    int i;
    int j;
    int k;
    int*q;
    int s;
    int z;
    int*d;
    if(a==b){
      return 0;
    }
    walloc1d(&d, N, &mem);
    walloc1d(&q, N, &mem);
    for(i=(0);i<(N);i++){
      d[i] = -1;
    }
    d[a] = 0;
    s = 0;
    z = 1;
    q[0] = a;
    while(z){
      i = q[s++];
      z--;
      for(j=(0);j<(es[i]);j++){
        k = edge[i][j];
        if(d[k] >= 0){
          continue;
        }
        d[k] = d[i] + 1;
        if(k==b){
          return d[k];
        }
        q[s+z++] = k;
      }
    }
    return -1;
  }
}
;
template<class T> struct wgraph{
  int N;
  int*es;
  int**edge;
  T**cost;
  graph g;
  void setDirectEdge(int N__, int M, int A[], int B[], T C[], void **mem = &wmem){
    int i;
    N = N__;
    walloc1d(&es, N, mem);
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      es[A[i]]++;
    }
    walloc1d(&edge, N, mem);
    for(i=(0);i<(N);i++){
      walloc1d(&edge[i], es[i], mem);
    }
    walloc1d(&cost, N, mem);
    for(i=(0);i<(N);i++){
      walloc1d(&cost[i], es[i], mem);
    }
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      edge[A[i]][es[A[i]]] = B[i];
      cost[A[i]][es[A[i]]++] = C[i];
    }
    g.N = N;
    g.es = es;
    g.edge = edge;
  }
  template<class S> void getDist(int root, S res[], S unreachable = -1, void *mem = wmem){
    int i;
    int j;
    DijkstraHeap<S> hp;
    hp.walloc(N, &mem);
    hp.init(N);
    hp.change(root,0);
    while(hp.size){
      i = hp.pop();
      for(j=(0);j<(es[i]);j++){
        hp.change(edge[i][j], hp.val[i]+cost[i][j]);
      }
    }
    for(i=(0);i<(N);i++){
      res[i] = (hp.visited[i] ? hp.val[i] : unreachable);
    }
  }
}
;
int H;
int W;
long long U;
long long D;
long long R;
long long L;
long long K;
long long P;
int xs;
int ys;
int xt;
int yt;
char C[110][110];
int vn[110][110];
int tvn;
int A[100000];
int B[100000];
int esz;
long long C2[100000];
void add_edge(int x, int y, long long z){
  arrInsert(esz, esz, A, x, B, y, C2, z);
}
wgraph<long long> g;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
long long costdir[4];
bool within(int x, int y){
  return (0 <= x  &&  x < H) && (0 <= y  &&  y < W);
}
long long res[100000];
int main(){
  int i;
  wmem = memarr;
  rd(H);
  rd(W);
  rd(U);
  rd(D);
  rd(R);
  rd(L);
  rd(K);
  rd(P);
  rd(xs);
  rd(ys);
  rd(xt);
  rd(yt);
  --xs;
  --ys;
  --xt;
  --yt;
  for(i=(0);i<(H);i++){
    rd(C[i]);
  }
  costdir[0] = D;
  costdir[1] = U;
  costdir[2] = R;
  costdir[3] = L;
  tvn = 0;
  for(i=(0);i<(H);i++){
    int j;
    for(j=(0);j<(W);j++){
      vn[i][j] = tvn++;
    }
  }
  for(i=(0);i<(H);i++){
    int j;
    for(j=(0);j<(W);j++){
      int k;
      for(k=(0);k<(4);k++){
        int ni = i + dx[k];
        int nj = j + dy[k];
        long long nc = costdir[k];
        if(!within(ni, nj)){
          continue;
        }
        if(C[ni][nj] == '#'){
          continue;
        }
        if(C[ni][nj] == '@'){
          nc += P;
        }
        add_edge(vn[i][j], vn[ni][nj], nc);
      }
    }
  }
  g.setDirectEdge(tvn, esz, A, B, C2);
  g.getDist(vn[xs][ys], res);
  if(res[vn[xt][yt]] == -1){
    wt_L("No");
    wt_L('\n');
    exit(0);
  }
  if(res[vn[xt][yt]] <= K){
    wt_L("Yes");
    wt_L('\n');
    exit(0);
  }
  wt_L("No");
  wt_L('\n');
  return 0;
}
// cLay version 20210405-1

// --- original code ---
// //no-unlocked
// 
// int H, W;
// ll U, D, R, L, K, P;
// int xs, ys, xt, yt;
// char C[110][110];
// int vn[110][110], tvn;
// 
// int A[1d5], B[], esz;
// ll C2[];
// 
// void add_edge(int x, int y, ll z){
//     arrInsert(esz, esz, A, x, B, y, C2, z);
// }
// 
// wgraph<ll> g;
// 
// const int dx[4] = {1, -1, 0, 0};
// const int dy[4] = {0, 0, 1, -1};
// ll costdir[4];
// bool within(int x, int y){
//     return (0 <= x < H) && (0 <= y < W);
// }
// //const char *  = "DURL";
// ll res[1d5];
// { 
//     rd(H, W);
//     rd(U, D, R, L, K, P);
//     rd(xs, ys, xt, yt);
//     --xs; --ys; --xt; --yt;
//     rep(i, H){
//         rd(C[i]);
//     }
//     costdir[0] = D;costdir[1] = U; costdir[2] = R;costdir[3] = L;
//     tvn = 0;
//     rep(i, H){
//         rep(j, W){
//             vn[i][j] = tvn++;
//         }
//     }
//     rep(i, H)
//         rep(j, W){
//             rep(k, 4){
//                 int ni = i + dx[k];
//                 int nj = j + dy[k];
//                 ll nc = costdir[k];
//                 if(!within(ni, nj)) continue;
//                 if(C[ni][nj] == '#') continue;
//                 if(C[ni][nj] == '@'){
//                     nc += P;
//                 }
//                 add_edge(vn[i][j], vn[ni][nj], nc);
//             }
//         }
//     g.setDirectEdge(tvn, esz, A, B, C2);
//     g.getDist(vn[xs][ys], res);
//     if(res[vn[xt][yt]] == -1){
//         wt("No");
//         exit(0);
//     }
//     if(res[vn[xt][yt]] <= K){
//         wt("Yes");
//         exit(0);
//     }
//     wt("No");
// }
0