結果

問題 No.867 避難経路
ユーザー DAyamaCTFDAyamaCTF
提出日時 2019-08-16 23:35:37
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 5,064 ms / 6,000 ms
コード長 2,180 bytes
コンパイル時間 2,584 ms
コンパイル使用メモリ 171,636 KB
実行使用メモリ 142,116 KB
最終ジャッジ日時 2023-10-24 06:29:22
合計ジャッジ時間 133,527 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
62,560 KB
testcase_01 AC 11 ms
9,688 KB
testcase_02 AC 19 ms
60,596 KB
testcase_03 AC 11 ms
9,688 KB
testcase_04 AC 19 ms
60,596 KB
testcase_05 AC 10 ms
9,688 KB
testcase_06 AC 20 ms
60,596 KB
testcase_07 AC 11 ms
9,688 KB
testcase_08 AC 19 ms
60,596 KB
testcase_09 AC 10 ms
9,688 KB
testcase_10 AC 19 ms
60,596 KB
testcase_11 AC 11 ms
9,688 KB
testcase_12 AC 19 ms
60,596 KB
testcase_13 AC 11 ms
9,688 KB
testcase_14 AC 19 ms
60,600 KB
testcase_15 AC 4,854 ms
141,604 KB
testcase_16 AC 4,744 ms
142,060 KB
testcase_17 AC 5,064 ms
142,088 KB
testcase_18 AC 4,619 ms
142,088 KB
testcase_19 AC 4,646 ms
142,088 KB
testcase_20 AC 4,547 ms
142,088 KB
testcase_21 AC 4,638 ms
142,088 KB
testcase_22 AC 4,647 ms
142,088 KB
testcase_23 AC 5,034 ms
142,088 KB
testcase_24 AC 4,571 ms
142,088 KB
testcase_25 AC 4,600 ms
142,088 KB
testcase_26 AC 4,659 ms
142,088 KB
testcase_27 AC 4,752 ms
142,088 KB
testcase_28 AC 4,500 ms
142,084 KB
testcase_29 AC 4,567 ms
142,084 KB
testcase_30 AC 4,429 ms
142,052 KB
testcase_31 AC 4,468 ms
142,088 KB
testcase_32 AC 4,517 ms
142,088 KB
testcase_33 AC 4,489 ms
142,088 KB
testcase_34 AC 4,482 ms
142,088 KB
testcase_35 AC 4,520 ms
142,088 KB
testcase_36 AC 4,425 ms
142,088 KB
testcase_37 AC 4,262 ms
142,076 KB
testcase_38 AC 4,272 ms
142,076 KB
testcase_39 AC 4,291 ms
142,104 KB
testcase_40 AC 4,319 ms
142,116 KB
testcase_41 AC 29 ms
140,820 KB
testcase_42 AC 31 ms
140,840 KB
testcase_43 AC 31 ms
140,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcountll

#define INF 1e16
#define mod 1000000007

struct state{
  ll i,j,d;
};
bool operator<(const state& a,const state& b){
  return a.d > b.d;
}
struct state2{
  ll i,j;
  P p;
};
bool operator<(const state2& a,const state2& b){
  return a.p > b.p;
}

ll H,W;
ll gi,gj;
ll A[250][250];
ll Q;
ll dist[300][250][250];
P dist2[250][250];
ll di[]={-1,0,+1,0};
ll dj[]={0,-1,0,+1};

const ll Kmax=280;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin>>H>>W>>gi>>gj;
  gi--;gj--;
  rep(i,H)rep(j,W){
    cin>>A[i][j];
  }

  repl(k,1,Kmax){
    rep(i,H)rep(j,W)dist[k][i][j]=INF;
    priority_queue<state> que;
    que.push((state){gi,gj,k*k+A[gi][gj]});
    while(que.size()){
      state s=que.top(); que.pop();
      if(dist[k][s.i][s.j]!=INF)continue;
      dist[k][s.i][s.j]=s.d;
      rep(dir,4){
        ll ni=s.i+di[dir],nj=s.j+dj[dir];
        if(ni<0||ni>=H||nj<0||nj>=W)continue;
        que.push((state){ni,nj,s.d+k*k+A[ni][nj]});
      }
    }
  }
  {
    rep(i,H)rep(j,W)dist2[i][j]=P(INF,INF);
    priority_queue<state2> que2;
    que2.push((state2){gi,gj,P(1,A[gi][gj])});
    while(que2.size()){
      state2 s=que2.top(); que2.pop();
      if(dist2[s.i][s.j]!=P(INF,INF))continue;
      dist2[s.i][s.j]=s.p;
      rep(dir,4){
        ll ni=s.i+di[dir],nj=s.j+dj[dir];
        if(ni<0||ni>=H||nj<0||nj>=W)continue;
        que2.push((state2){ni,nj,P(s.p.fi+1,s.p.se+A[ni][nj])});
      }
    }
  }

  cin>>Q;
  rep(_,Q){
    ll x,y,k;
    cin>>x>>y>>k;
    x--;y--;
    if(k<Kmax) cout<<dist[k][x][y]<<'\n';
    else{
      P p=dist2[x][y];
      cout<<p.fi*k*k+p.se<<'\n';
    }
  }

  return 0;
}
0