結果

問題 No.971 いたずらっ子
ユーザー LayCurseLayCurse
提出日時 2020-01-17 23:09:17
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 763 ms / 2,000 ms
コード長 4,741 bytes
コンパイル時間 2,399 ms
コンパイル使用メモリ 208,616 KB
実行使用メモリ 42,752 KB
最終ジャッジ日時 2024-04-25 02:03:10
合計ジャッジ時間 8,372 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 763 ms
42,624 KB
testcase_01 AC 762 ms
42,752 KB
testcase_02 AC 538 ms
34,048 KB
testcase_03 AC 513 ms
42,496 KB
testcase_04 AC 470 ms
40,320 KB
testcase_05 AC 503 ms
42,624 KB
testcase_06 AC 374 ms
34,304 KB
testcase_07 AC 5 ms
5,376 KB
testcase_08 AC 114 ms
14,848 KB
testcase_09 AC 103 ms
14,208 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 4 ms
7,552 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 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);
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void rd(char &c){
  int i;
  for(;;){
    i = getchar_unlocked();
    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 = getchar_unlocked();
    if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF){
      break;
    }
  }
  c[sz++] = i;
  for(;;){
    i = getchar_unlocked();
    if(i==' '||i=='\n'||i=='\r'||i=='\t'||i==EOF){
      break;
    }
    c[sz++] = i;
  }
  c[sz]='\0';
  return sz;
}
inline void wt_L(char a){
  putchar_unlocked(a);
}
inline void wt_L(int x){
  int s=0;
  int m=0;
  char f[10];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    putchar_unlocked('-');
  }
  while(s--){
    putchar_unlocked(f[s]+'0');
  }
}
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;
  }
}
;
int X;
int Y;
char A[2000][2002];
int main(){
  wmem = memarr;
  int k;
  int sx;
  int sy;
  int nx;
  int ny;
  int dist;
  int ndist;
  int cost;
  int ncost;
  int d;
  DijkstraHeap<int> hp;
  int dx[2] = {1,0};
  int dy[2] = {0,1};
  int res = 1073709056;
  rd(X);
  rd(Y);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(X);Lj4PdHRW++){
      rd(A[Lj4PdHRW]);
    }
  }
  hp.malloc(X*Y);
  hp.init(X*Y);
  hp.change(0,0);
  while(hp.size){
    k = hp.pop();
    sx = k / Y;
    sy = k % Y;
    for(d=(0);d<(2);d++){
      nx = sx + dx[d];
      ny = sy + dy[d];
      if(nx < 0 || ny < 0 || nx >= X || ny >= Y){
        continue;
      }
      if(A[nx][ny]=='k'){
        hp.change(nx*Y+ny, hp.val[k] +nx+ny+1);
      }
      else{
        hp.change(nx*Y+ny, hp.val[k] +1);
      }
    }
  }
  res = hp.val[(X-1)*Y+Y-1];
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay varsion 20200112-1

// --- original code ---
// int X, Y;
// char A[2000][2002];
// 
// {
//   int k, sx, sy, nx, ny, dist, ndist, cost, ncost, d;
//   DijkstraHeap<int> hp;
//   int dx[2] = {1,0};
//   int dy[2] = {0,1};
//   int res = int_inf;
//   rd(X,Y,A(X));
//   hp.malloc(X*Y);
//   hp.init(X*Y);
//   hp.change(0,0);
//   while(hp.size){
//     k = hp.pop();
//     sx = k / Y;
//     sy = k % Y;
//     rep(d,2){
//       nx = sx + dx[d];
//       ny = sy + dy[d];
//       if(nx < 0 || ny < 0 || nx >= X || ny >= Y) continue;
//       hp.change(nx*Y+ny, hp.val[k] + if[A[nx][ny]=='k', nx+ny+1, 1]);
//     }
//   }
//   res = hp.val[(X-1)*Y+Y-1];
//   wt(res);
// }
0