結果

問題 No.867 避難経路
ユーザー LayCurseLayCurse
提出日時 2019-08-16 22:56:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4,123 ms / 6,000 ms
コード長 5,295 bytes
コンパイル時間 2,481 ms
コンパイル使用メモリ 205,956 KB
実行使用メモリ 127,836 KB
最終ジャッジ日時 2023-10-24 03:46:59
合計ジャッジ時間 96,602 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
126,640 KB
testcase_01 AC 29 ms
126,640 KB
testcase_02 AC 29 ms
126,640 KB
testcase_03 AC 29 ms
126,640 KB
testcase_04 AC 29 ms
126,640 KB
testcase_05 AC 29 ms
126,640 KB
testcase_06 AC 29 ms
126,640 KB
testcase_07 AC 32 ms
126,640 KB
testcase_08 AC 29 ms
126,640 KB
testcase_09 AC 29 ms
126,640 KB
testcase_10 AC 29 ms
126,640 KB
testcase_11 AC 28 ms
126,640 KB
testcase_12 AC 29 ms
126,640 KB
testcase_13 AC 29 ms
126,640 KB
testcase_14 AC 29 ms
126,640 KB
testcase_15 AC 3,899 ms
127,836 KB
testcase_16 AC 3,748 ms
127,836 KB
testcase_17 AC 4,123 ms
127,836 KB
testcase_18 AC 3,745 ms
127,836 KB
testcase_19 AC 3,562 ms
127,836 KB
testcase_20 AC 3,606 ms
127,836 KB
testcase_21 AC 3,334 ms
127,836 KB
testcase_22 AC 3,588 ms
127,836 KB
testcase_23 AC 4,090 ms
127,836 KB
testcase_24 AC 3,383 ms
127,836 KB
testcase_25 AC 3,653 ms
127,836 KB
testcase_26 AC 3,773 ms
127,836 KB
testcase_27 AC 3,775 ms
127,836 KB
testcase_28 AC 3,229 ms
127,836 KB
testcase_29 AC 3,452 ms
127,836 KB
testcase_30 AC 2,756 ms
127,836 KB
testcase_31 AC 2,975 ms
127,780 KB
testcase_32 AC 2,914 ms
127,828 KB
testcase_33 AC 2,894 ms
127,828 KB
testcase_34 AC 2,770 ms
127,820 KB
testcase_35 AC 2,880 ms
127,820 KB
testcase_36 AC 2,851 ms
127,808 KB
testcase_37 AC 2,671 ms
127,748 KB
testcase_38 AC 2,651 ms
127,748 KB
testcase_39 AC 2,634 ms
127,752 KB
testcase_40 AC 2,623 ms
127,752 KB
testcase_41 AC 27 ms
124,632 KB
testcase_42 AC 27 ms
124,648 KB
testcase_43 AC 27 ms
124,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 500
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 20190810-2

// --- original code ---
// #define L 500
// 
// 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