結果

問題 No.867 避難経路
ユーザー betrue12betrue12
提出日時 2019-08-16 23:12:57
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 3,447 ms / 6,000 ms
コード長 2,595 bytes
コンパイル時間 2,510 ms
コンパイル使用メモリ 187,104 KB
実行使用メモリ 78,700 KB
最終ジャッジ日時 2023-10-24 05:02:57
合計ジャッジ時間 59,500 ms
ジャッジサーバーID
(参考情報)
judge12 / 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 3 ms
4,348 KB
testcase_12 AC 3 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 1,278 ms
38,668 KB
testcase_16 AC 1,248 ms
38,308 KB
testcase_17 AC 1,268 ms
38,308 KB
testcase_18 AC 1,222 ms
38,572 KB
testcase_19 AC 1,241 ms
38,308 KB
testcase_20 AC 521 ms
35,684 KB
testcase_21 AC 508 ms
35,936 KB
testcase_22 AC 591 ms
35,884 KB
testcase_23 AC 588 ms
35,764 KB
testcase_24 AC 593 ms
35,720 KB
testcase_25 AC 851 ms
39,364 KB
testcase_26 AC 854 ms
39,364 KB
testcase_27 AC 809 ms
39,364 KB
testcase_28 AC 846 ms
39,628 KB
testcase_29 AC 854 ms
39,364 KB
testcase_30 AC 3,137 ms
78,700 KB
testcase_31 AC 3,226 ms
39,100 KB
testcase_32 AC 3,194 ms
38,836 KB
testcase_33 AC 3,169 ms
39,364 KB
testcase_34 AC 3,273 ms
78,700 KB
testcase_35 AC 3,447 ms
78,700 KB
testcase_36 AC 3,442 ms
78,692 KB
testcase_37 AC 3,250 ms
78,700 KB
testcase_38 AC 3,256 ms
78,700 KB
testcase_39 AC 3,172 ms
78,700 KB
testcase_40 AC 3,175 ms
78,700 KB
testcase_41 AC 2 ms
4,372 KB
testcase_42 AC 2 ms
4,372 KB
testcase_43 AC 2 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

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] = K2 + 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