結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
41,484 KB
testcase_01 AC 11 ms
10,152 KB
testcase_02 AC 10 ms
10,152 KB
testcase_03 AC 10 ms
10,152 KB
testcase_04 AC 11 ms
10,152 KB
testcase_05 AC 11 ms
10,152 KB
testcase_06 AC 11 ms
10,152 KB
testcase_07 AC 12 ms
10,152 KB
testcase_08 AC 10 ms
10,152 KB
testcase_09 AC 10 ms
10,152 KB
testcase_10 AC 10 ms
10,152 KB
testcase_11 AC 10 ms
10,152 KB
testcase_12 AC 10 ms
10,152 KB
testcase_13 AC 10 ms
10,152 KB
testcase_14 AC 11 ms
10,156 KB
testcase_15 AC 5,334 ms
151,204 KB
testcase_16 AC 5,113 ms
151,676 KB
testcase_17 AC 5,471 ms
151,704 KB
testcase_18 AC 5,008 ms
151,704 KB
testcase_19 AC 5,075 ms
151,704 KB
testcase_20 AC 4,942 ms
151,704 KB
testcase_21 AC 5,107 ms
151,704 KB
testcase_22 AC 5,058 ms
151,704 KB
testcase_23 AC 5,427 ms
151,704 KB
testcase_24 AC 4,979 ms
151,704 KB
testcase_25 AC 4,985 ms
151,704 KB
testcase_26 AC 4,952 ms
151,704 KB
testcase_27 AC 5,144 ms
151,704 KB
testcase_28 AC 4,863 ms
151,700 KB
testcase_29 AC 4,951 ms
151,700 KB
testcase_30 AC 4,775 ms
151,668 KB
testcase_31 AC 4,858 ms
151,704 KB
testcase_32 AC 4,850 ms
151,704 KB
testcase_33 AC 4,848 ms
151,704 KB
testcase_34 AC 4,782 ms
151,704 KB
testcase_35 AC 4,807 ms
151,704 KB
testcase_36 AC 4,732 ms
151,624 KB
testcase_37 AC 4,551 ms
151,176 KB
testcase_38 AC 4,542 ms
151,176 KB
testcase_39 AC 4,542 ms
151,204 KB
testcase_40 AC 4,550 ms
151,216 KB
testcase_41 AC 29 ms
133,016 KB
testcase_42 AC 31 ms
133,252 KB
testcase_43 AC 30 ms
133,252 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=300;

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