結果

問題 No.867 避難経路
ユーザー LayCurseLayCurse
提出日時 2019-08-21 06:03:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,076 ms / 6,000 ms
コード長 5,318 bytes
コンパイル時間 2,963 ms
コンパイル使用メモリ 210,928 KB
実行使用メモリ 60,800 KB
最終ジャッジ日時 2024-04-16 17:36:03
合計ジャッジ時間 55,722 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
6,400 KB
testcase_01 AC 6 ms
6,656 KB
testcase_02 AC 6 ms
6,400 KB
testcase_03 AC 6 ms
6,528 KB
testcase_04 AC 6 ms
6,400 KB
testcase_05 AC 6 ms
6,656 KB
testcase_06 AC 6 ms
6,528 KB
testcase_07 AC 5 ms
6,528 KB
testcase_08 AC 5 ms
6,528 KB
testcase_09 AC 5 ms
6,656 KB
testcase_10 AC 6 ms
6,528 KB
testcase_11 AC 6 ms
6,656 KB
testcase_12 AC 6 ms
6,528 KB
testcase_13 AC 6 ms
6,656 KB
testcase_14 AC 6 ms
6,528 KB
testcase_15 AC 2,048 ms
60,544 KB
testcase_16 AC 1,881 ms
60,672 KB
testcase_17 AC 2,076 ms
60,672 KB
testcase_18 AC 1,860 ms
60,672 KB
testcase_19 AC 1,840 ms
60,800 KB
testcase_20 AC 1,867 ms
60,544 KB
testcase_21 AC 1,755 ms
60,544 KB
testcase_22 AC 1,786 ms
60,672 KB
testcase_23 AC 1,958 ms
60,544 KB
testcase_24 AC 1,674 ms
60,544 KB
testcase_25 AC 1,781 ms
60,672 KB
testcase_26 AC 1,896 ms
60,544 KB
testcase_27 AC 1,845 ms
60,672 KB
testcase_28 AC 1,575 ms
60,672 KB
testcase_29 AC 1,628 ms
60,544 KB
testcase_30 AC 1,406 ms
60,416 KB
testcase_31 AC 1,557 ms
60,672 KB
testcase_32 AC 1,490 ms
60,532 KB
testcase_33 AC 1,473 ms
60,672 KB
testcase_34 AC 1,419 ms
60,672 KB
testcase_35 AC 1,459 ms
60,672 KB
testcase_36 AC 1,451 ms
60,544 KB
testcase_37 AC 1,367 ms
59,520 KB
testcase_38 AC 1,353 ms
59,392 KB
testcase_39 AC 1,367 ms
59,264 KB
testcase_40 AC 1,341 ms
59,520 KB
testcase_41 AC 3 ms
5,376 KB
testcase_42 AC 4 ms
5,760 KB
testcase_43 AC 4 ms
5,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void *wmem;
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, 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 wt_L(char a){
  putchar_unlocked(a);
}
inline void wt_L(int x){
  char f[10];
  int m=0, s=0;
  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');
  }
}
inline void wt_L(long long x){
  char f[20];
  int m=0, s=0;
  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{
  T *val;
  char *visited;
  int *hp, *place, size;
  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;
  }
}
;
char memarr[96000000];
#define L 230
int X;
int Y;
int GX;
int GY;
int A[250][250];
int Q;
int dist[L][250][250];
int main(){
  DijkstraHeap<int> hp;
  int KL2GvlyY, d, di[4]={-1,1,0,0}, dj[4]={0,0,-1,1}, i, j, k, ni, nj, si, sj;
  long long res;
  wmem = memarr;
  rd(X);
  rd(Y);
  rd(GX);GX += (-1);
  rd(GY);GY += (-1);
  for(i=0;i<(X);i++){
    {
      int Lj4PdHRW;
      for(Lj4PdHRW=0;Lj4PdHRW<(Y);Lj4PdHRW++){
        rd(A[i][Lj4PdHRW]);
      }
    }
  }
  hp.malloc(X*Y);
  for(k=0;k<(L);k++){
    hp.init(X*Y);
    hp.change(GX*Y+GY, (k+1) * (k+1) + A[GX][GY]);
    while(hp.size){
      i = hp.pop();
      si = i / Y;
      sj = i % Y;
      for(d=0;d<(4);d++){
        ni = si + di[d];
        nj = sj + dj[d];
        if(ni < 0 || nj < 0 || ni >= X || nj >= Y){
          continue;
        }
        hp.change(ni*Y+nj, hp.val[i] + (k+1) * (k+1) + A[ni][nj]);
      }
    }
    for(i=0;i<(X);i++){
      for(j=0;j<(Y);j++){
        dist[k][i][j] = hp.val[i*Y+j];
      }
    }
  }
  rd(Q);
  for(KL2GvlyY=0;KL2GvlyY<(Q);KL2GvlyY++){
    rd(i);i += (-1);
    rd(j);j += (-1);
    rd(k);
    if(k <= L){
      wt_L(dist[k-1][i][j]);
      wt_L('\n');
    }
    else{
      res = dist[L-1][i][j] % (L*L);
      res += (long long)(dist[L-1][i][j] / (L*L)) * k * k;
      wt_L(res);
      wt_L('\n');
    }
  }
  return 0;
}
// cLay varsion 20190820-1

// --- original code ---
// #define L 230
// 
// int X, Y, GX, GY, A[250][250], Q;
// 
// int dist[L][250][250];
// 
// {
//   int i, j, k, d;
//   int di[4] = {-1,1,0,0}, dj[4] = {0,0,-1,1};
//   int si, sj, ni, nj;
//   DijkstraHeap<int> hp;
//   ll res;
// 
//   rd(X,Y,GX--,GY--);
//   rep(i,X) rd(A[i](Y));
// 
//   hp.malloc(X*Y);
//   rep(k,L){
//     hp.init(X*Y);
//     hp.change(GX*Y+GY, (k+1) * (k+1) + A[GX][GY]);
//     while(hp.size){
//       i = hp.pop();
//       si = i / Y;
//       sj = i % Y;
//       rep(d,4){
//         ni = si + di[d];
//         nj = sj + dj[d];
//         if(ni < 0 || nj < 0 || ni >= X || nj >= Y) continue;
//         hp.change(ni*Y+nj, hp.val[i] + (k+1) * (k+1) + A[ni][nj]);
//       }
//     }
//     rep(i,X) rep(j,Y) dist[k][i][j] = hp.val[i*Y+j];
//   }
// 
//   rd(Q);
//   rep(Q){
//     rd(i--, j--, k);
//     if(k <= L){
//       wt(dist[k-1][i][j]);
//     } else {
//       res = dist[L-1][i][j] % (L*L);
//       res += (ll)(dist[L-1][i][j] / (L*L)) * k * k;
//       wt(res);
//     }
//   }
// }
0