結果

問題 No.867 避難経路
ユーザー fumofumofunifumofumofuni
提出日時 2023-07-01 12:20:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,089 bytes
コンパイル時間 2,455 ms
コンパイル使用メモリ 212,016 KB
実行使用メモリ 161,388 KB
最終ジャッジ日時 2023-09-22 06:08:57
合計ジャッジ時間 11,472 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
161,344 KB
testcase_01 AC 70 ms
161,132 KB
testcase_02 AC 73 ms
161,388 KB
testcase_03 AC 72 ms
161,120 KB
testcase_04 AC 73 ms
161,184 KB
testcase_05 AC 72 ms
161,112 KB
testcase_06 AC 71 ms
161,300 KB
testcase_07 AC 71 ms
161,304 KB
testcase_08 AC 71 ms
161,180 KB
testcase_09 AC 72 ms
161,236 KB
testcase_10 AC 72 ms
161,296 KB
testcase_11 AC 73 ms
161,372 KB
testcase_12 AC 72 ms
161,208 KB
testcase_13 AC 72 ms
161,236 KB
testcase_14 AC 73 ms
161,164 KB
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 AC 47 ms
134,456 KB
testcase_42 AC 58 ms
148,040 KB
testcase_43 AC 58 ms
147,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={-1,0,1,0,1,1,-1,-1};
const ll dx[8]={0,-1,0,1,1,-1,1,-1};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
const ll M=2500;
ll dp[M][250][250];
int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  ll h,w;cin >> h >> w;
  ll x,y;cin >> x >> y;x--;y--;
  vvl g(h,vl(w));rep(i,h)rep(j,w)cin >> g[i][j];
  if(h*w>25000){
    vl mle(INF);return 0;
  }
  rep(k,M){
    rep(i,h)rep(j,w)dp[k][i][j]=INF;
    dp[k][x][y]=g[x][y]+k*k;
    priority_queue<pl,vpl,greater<pl>> que;
    que.push({g[x][y]+k*k,x*w+y});
    while(que.size()){
      auto [dist,v]=que.top();que.pop();
      ll nx=v/w,ny=v%w;
      if(dp[k][nx][ny]!=dist)continue;
      rep(i,4){
        ll nnx=nx+dx[i],nny=ny+dy[i];
        if(nnx<0||nny<0||nnx>=h||nny>=w)continue;
        ll ncost=dist+g[nnx][nny]+k*k;
        if(dp[k][nnx][nny]<=ncost)continue;
        dp[k][nnx][nny]=ncost;
        que.push({ncost,nnx*w+nny});
      }
    }
  }
  ll q;cin >> q;
  while(q--){
    ll nx,ny,k;cin >> nx >> ny >> k;nx--;ny--;
    if(k<M)cout << dp[k][nx][ny] << endl;
    else{
      ll f=dp[M-1][nx][ny]+(abs(nx-x)+abs(ny-y))*(k*k-(M-1)*(M-1));
      cout << f << endl;
    }
  }
}
0