結果

問題 No.867 避難経路
ユーザー tempura_pptempura_pp
提出日時 2019-08-17 00:11:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,499 ms / 6,000 ms
コード長 1,977 bytes
コンパイル時間 2,310 ms
コンパイル使用メモリ 108,228 KB
実行使用メモリ 29,572 KB
最終ジャッジ日時 2023-10-24 12:52:55
合計ジャッジ時間 35,786 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 1,146 ms
27,936 KB
testcase_16 AC 1,131 ms
27,540 KB
testcase_17 AC 1,180 ms
27,556 KB
testcase_18 AC 1,138 ms
27,452 KB
testcase_19 AC 1,114 ms
27,404 KB
testcase_20 AC 444 ms
24,140 KB
testcase_21 AC 443 ms
24,192 KB
testcase_22 AC 478 ms
29,264 KB
testcase_23 AC 480 ms
29,572 KB
testcase_24 AC 475 ms
29,232 KB
testcase_25 AC 536 ms
23,996 KB
testcase_26 AC 537 ms
24,016 KB
testcase_27 AC 540 ms
23,992 KB
testcase_28 AC 532 ms
24,004 KB
testcase_29 AC 541 ms
23,980 KB
testcase_30 AC 1,416 ms
23,868 KB
testcase_31 AC 1,403 ms
26,804 KB
testcase_32 AC 1,385 ms
26,748 KB
testcase_33 AC 1,367 ms
26,724 KB
testcase_34 AC 1,430 ms
23,888 KB
testcase_35 AC 1,499 ms
23,888 KB
testcase_36 AC 1,489 ms
23,880 KB
testcase_37 AC 1,430 ms
23,856 KB
testcase_38 AC 1,434 ms
23,856 KB
testcase_39 AC 1,418 ms
23,856 KB
testcase_40 AC 1,404 ms
23,856 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 2 ms
4,376 KB
testcase_43 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;

struct query{
    ll sx,sy,id,k;
};

int main(){
    int h,w;
    cin>>h>>w;
    ll a[h][w];
    int gx,gy;
    cin>>gx>>gy;
    --gx;--gy;
    rep(i,h)rep(j,w)cin>>a[i][j];
    int q;
    cin>>q;
    vector<query> v[161];
    rep(i,q){
        int x,y,k;
        cin>>x>>y>>k;
        --x;--y;
        v[min(k,160)].push_back({x,y,i,k});
    }
    ll dist[300][300];
    int di[]={1,0,-1,0,1};
    vector<ll> ans(q);
    rep(K,161){
        if(v[K].empty())continue;
        rep(i,h)rep(j,w){
            dist[i][j]=longinf;
        }
        dist[gx][gy]=K*K+a[gx][gy];
        using T  =pair<ll,pair<int,int>>;
        priority_queue<T,vector<T>, greater<T>> pq;
        pq.push({dist[gx][gy],{gx,gy}});
        while(pq.size()){
            ll d = pq.top().first;
            auto p = pq.top().second;
            pq.pop();
            int x = p.first, y = p.second;
            if(dist[x][y]<d)continue;
            rep(i,4){
                int nx = x+di[i], ny=y+di[i+1]; 
                if(nx<0||nx>=h||ny<0||ny>=w)continue;
                if(dist[nx][ny]>d+a[nx][ny]+K*K){
                    dist[nx][ny]=d+a[nx][ny]+K*K;
                    pq.push({dist[nx][ny],{nx,ny}});
                }
            }
        }
        for(auto Q :v[K]){
            ans[Q.id]=dist[Q.sx][Q.sy];
            if(K!=Q.k){
                ll d = abs(Q.sx-gx)+abs(Q.sy-gy)+1;
                ans[Q.id]+=(Q.k*Q.k-K*K)*d;
            }
        }
    }
    for(auto e : ans)cout<<e<<"\n";
    return 0;
}
0