結果

問題 No.867 避難経路
ユーザー IKyoproIKyopro
提出日時 2019-08-16 23:49:40
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,933 bytes
コンパイル時間 1,717 ms
コンパイル使用メモリ 76,300 KB
実行使用メモリ 364,984 KB
最終ジャッジ日時 2023-10-24 09:59:48
合計ジャッジ時間 110,409 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 3 ms
13,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;

ll dp[251][251][701] = {};
ll inf = 1e18;
int dx[4] = {-1,1,0,0},dy[4] = {0,0,-1,1};

int main(){
    int H,W,gx,gy;
    cin >> H >> W >> gx >> gy;
    int K = min(H*W,700);
    vector<vector<ll>> A(H+1,vector<ll>(W+1,0));
    for(int i=1;i<=H;i++) for(int j=1;j<=W;j++){
        cin >> A[i][j];
        for(int k=0;k<=K;k++) dp[i][j][k] = inf;
    }
    dp[gx][gy][1] = A[gx][gy];
    auto in = [&](int x,int y){
        return 1<=x && x<=H && 1<=y && y<=W;
    };
    for(int k=1;k<K;k++) for(int i=1;i<=H;i++) for(int j=1;j<=W;j++){
        for(int l=0;l<4;l++){
            int nx = i+dx[l],ny = j+dy[l];
            if(!in(nx,ny)) continue;
            dp[nx][ny][k+1] = min(dp[nx][ny][k+1],dp[i][j][k]+A[nx][ny]);
        }
    }
    int Q;
    cin >> Q;
    vector<ll> ans(Q,0);
    struct query{
        int id,x,y;
        ll level;
        bool operator<(const query& right)const{
            return level<right.level;
        }
    };
    vector<vector<ll>> ans_id(H+1,vector<ll>(W+1,K));
    vector<vector<ll>> ans_now(H+1,vector<ll>(W+1,inf));
    vector<query> qs;
    for(int i=1;i<=H;i++) for(int j=1;j<=W;j++){
        if((abs(i-gx)+abs(j-gy))%2==1) ans_id[i][j] = K-1;
    }
    for(int q=0;q<Q;q++){
        int x,y; ll k;
        cin >> x >> y >> k;
        qs.push_back({q,x,y,k});
    }
    sort(qs.begin(),qs.end());
    for(auto q:qs){
        ans_now[q.x][q.y] = dp[q.x][q.y][ans_id[q.x][q.y]]+ans_id[q.x][q.y]*q.level*q.level;
        while(ans_id[q.x][q.y]>=2 && ans_now[q.x][q.y]>dp[q.x][q.y][ans_id[q.x][q.y]-2]+(ans_id[q.x][q.y]-2)*q.level*q.level){
            ans_now[q.x][q.y] = dp[q.x][q.y][ans_id[q.x][q.y]-2]+(ans_id[q.x][q.y]-2)*q.level*q.level;
            ans_id[q.x][q.y] -= 2;
        }
        ans[q.id] = ans_now[q.x][q.y];
    }
    for(auto x:ans) cout << x << endl;
}
0