結果
| 問題 |
No.867 避難経路
|
| コンテスト | |
| ユーザー |
LayCurse
|
| 提出日時 | 2019-08-16 22:55:23 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 5,286 bytes |
| コンパイル時間 | 2,389 ms |
| コンパイル使用メモリ | 196,792 KB |
| 最終ジャッジ日時 | 2025-01-07 12:40:41 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 21 TLE * 20 |
ソースコード
#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];
int X;
int Y;
int GX;
int GY;
int A[250][250];
int Q;
int dist[1000][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<(1000);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 <= 1000){
wt_L(dist[k-1][i][j]);
wt_L('\n');
}
else{
res = dist[999][i][j] % 1000000;
res += (long long)(dist[999][i][j] / 1000000) * k * k;
wt_L(res);
wt_L('\n');
}
}
return 0;
}
// cLay varsion 20190810-2
// --- original code ---
// int X, Y, GX, GY, A[250][250], Q;
//
// int dist[1000][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,1000){
// 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 <= 1000){
// wt(dist[k-1][i][j]);
// } else {
// res = dist[999][i][j] % 1000000;
// res += (ll)(dist[999][i][j] / 1000000) * k * k;
// wt(res);
// }
//
// }
// }
LayCurse