結果

問題 No.867 避難経路
ユーザー betrue12betrue12
提出日時 2019-08-16 23:10:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,594 bytes
コンパイル時間 1,942 ms
コンパイル使用メモリ 187,224 KB
実行使用メモリ 78,792 KB
最終ジャッジ日時 2023-10-24 04:41:25
合計ジャッジ時間 55,601 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int H, W, gx, gy;
bool inside(int i, int j){
    return i>=0 && i<H && j>=0 && j<W;
}
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};

int main(){
    cin >> H >> W >> gx >> gy;
    gx--; gy--;
    int A[250][250];
    for(int i=0; i<H; i++) for(int j=0; j<W; j++) cin >> A[i][j];

    int64_t mins[250][250];
    const int64_t OFFSET = 1e12;
    for(int i=0; i<H; i++) for(int j=0; j<W; j++) mins[i][j] = 1e18;
    mins[gx][gy] = OFFSET + A[gx][gy];
    typedef pair<int64_t, pair<int, int>> PP;
    priority_queue<PP, vector<PP>, greater<PP>> que;
    que.push({mins[gx][gy], {gx, gy}});
    while(que.size()){
        auto p = que.top(); que.pop();
        int64_t d = p.first;
        int i = p.second.first, j = p.second.second;
        if(d > mins[i][j]) continue;
        for(int k=0; k<4; k++){
            int x = i+dx[k], y = j+dy[k];
            if(inside(x, y) && mins[x][y] > d + OFFSET + A[x][y]){
                mins[x][y] = d + OFFSET + A[x][y];
                que.push({mins[x][y], {x, y}});
            }
        }
    }

    int Q;
    cin >> Q;
    map<int, vector<vector<int>>> qs;
    for(int i=0; i<Q; i++){
        int x, y, k;
        cin >> x >> y >> k;
        qs[k].push_back({i, x-1, y-1});
    }
    vector<int64_t> ans(Q);
    int64_t dist[250][250];
    for(auto& q : qs){
        int64_t K = q.first;
        int64_t K2 = K*K;
        if(K <= 500){
            for(int i=0; i<H; i++) for(int j=0; j<W; j++) dist[i][j] = 1e18;
            dist[gx][gy] = K + A[gx][gy];
            priority_queue<PP, vector<PP>, greater<PP>> que;
            que.push({dist[gx][gy], {gx, gy}});
            while(que.size()){
                auto p = que.top(); que.pop();
                int64_t d = p.first;
                int i = p.second.first, j = p.second.second;
                if(d > dist[i][j]) continue;
                for(int k=0; k<4; k++){
                    int x = i+dx[k], y = j+dy[k];
                    if(inside(x, y) && dist[x][y] > d + K2 + A[x][y]){
                        dist[x][y] = d + K2 + A[x][y];
                        que.push({dist[x][y], {x, y}});
                    }
                }
            }
            for(auto& v : q.second){
                ans[v[0]] = dist[v[1]][v[2]];
            }
        }else{
            for(auto& v : q.second){
                int i = v[1], j = v[2];
                ans[v[0]] = mins[i][j] - (OFFSET - K2) * (abs(i-gx) + abs(j-gy) + 1);
            }
        }
    }
    for(int i=0; i<Q; i++) cout << ans[i] << "\n";
    return 0;
}
0