結果

問題 No.867 避難経路
ユーザー tempura_pptempura_pp
提出日時 2019-08-16 23:30:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,997 bytes
コンパイル時間 1,714 ms
コンパイル使用メモリ 130,808 KB
実行使用メモリ 27,932 KB
最終ジャッジ日時 2023-10-24 05:58:09
合計ジャッジ時間 18,901 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
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 RE -
testcase_42 RE -
testcase_43 RE -
権限があれば一括ダウンロードができます

ソースコード

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 int long long
#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;
};

signed 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,300ll)].push_back({x,y,i,k});
    }
    ll dist[h][w];
    int di[]={1,0,-1,0,1};
    vector<ll> ans(q);
    rep(K,301){
        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)+(Q.sy-gy)+1;
                ans[Q.id]+=(Q.k*Q.k-K*K)*d;
            }
        }
    }
    for(auto e : ans)cout<<e<<"\n";
    return 0;
}
0