結果

問題 No.867 避難経路
ユーザー vwxyzvwxyz
提出日時 2024-04-14 17:08:13
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3,336 ms / 6,000 ms
コード長 1,987 bytes
コンパイル時間 1,893 ms
コンパイル使用メモリ 145,392 KB
実行使用メモリ 18,800 KB
最終ジャッジ日時 2024-04-14 17:09:42
合計ジャッジ時間 88,746 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,816 KB
testcase_01 AC 3 ms
6,940 KB
testcase_02 AC 4 ms
6,940 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 3 ms
6,944 KB
testcase_05 AC 4 ms
6,940 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 4 ms
6,944 KB
testcase_08 AC 4 ms
6,940 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 4 ms
6,940 KB
testcase_11 AC 3 ms
6,940 KB
testcase_12 AC 4 ms
6,940 KB
testcase_13 AC 3 ms
6,944 KB
testcase_14 AC 4 ms
6,940 KB
testcase_15 AC 3,206 ms
18,748 KB
testcase_16 AC 3,278 ms
18,784 KB
testcase_17 AC 3,276 ms
18,800 KB
testcase_18 AC 3,180 ms
18,560 KB
testcase_19 AC 3,197 ms
18,604 KB
testcase_20 AC 3,096 ms
15,840 KB
testcase_21 AC 3,100 ms
15,696 KB
testcase_22 AC 3,224 ms
17,280 KB
testcase_23 AC 3,237 ms
17,984 KB
testcase_24 AC 3,133 ms
17,820 KB
testcase_25 AC 3,265 ms
15,824 KB
testcase_26 AC 3,336 ms
15,996 KB
testcase_27 AC 3,156 ms
15,836 KB
testcase_28 AC 2,787 ms
15,852 KB
testcase_29 AC 3,016 ms
15,948 KB
testcase_30 AC 2,794 ms
15,648 KB
testcase_31 AC 2,808 ms
17,328 KB
testcase_32 AC 2,801 ms
16,460 KB
testcase_33 AC 2,811 ms
17,548 KB
testcase_34 AC 2,835 ms
15,720 KB
testcase_35 AC 2,949 ms
15,692 KB
testcase_36 AC 2,886 ms
15,752 KB
testcase_37 AC 2,841 ms
15,660 KB
testcase_38 AC 2,735 ms
15,656 KB
testcase_39 AC 2,792 ms
15,692 KB
testcase_40 AC 2,712 ms
15,784 KB
testcase_41 AC 2 ms
6,944 KB
testcase_42 AC 2 ms
6,940 KB
testcase_43 AC 3 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
    int H, W;
    cin >> H >> W;
    int gx, gy;
    cin >> gx >> gy;
    gx--; gy--;
    vector<vector<int>> A(H, vector<int>(W));
    for (int h = 0; h < H; ++h) {
        for (int w = 0; w < W; ++w) {
            cin >> A[h][w];
        }
    }
    int Q;
    cin >> Q;
    const int K = 251;
    vector<vector<tuple<int,int,int,int>>> query(K + 1);
    for (int q = 0; q < Q; ++q) {
        int x, y, k;
        cin >> x >> y >> k;
        x--; y--;
        query[min(k, K)].push_back({x, y, k, q});
    }
    vector<long long> ans_lst(Q);
    for (int k = 0; k <= K; ++k) {
        long long w = k < K ? k * k : (1 << 30);
        long long inf = (1LL << 60);
        vector<vector<long long>> dist(H, vector<long long>(W, inf));
        dist[gx][gy] = 0;
        priority_queue<tuple<long long, int, int>> queue;
        queue.push({-dist[gx][gy], gx, gy});
        while (!queue.empty()) {
            auto [d, x, y] = queue.top();
            queue.pop();
            d = -d;
            for (auto [dx, dy] : vector<pair<int, int>>{{0,1},{1,0},{0,-1},{-1,0}}) {
                int xx = x + dx, yy = y + dy;
                if (0 <= xx && xx < H && 0 <= yy && yy < W) {
                    long long dd = d + w + A[xx][yy];
                    if (dist[xx][yy] > dd) {
                        dist[xx][yy] = dd;
                        queue.push({-dd, xx, yy});
                    }
                }
            }
        }
        for (auto [x, y, c, q] : query[k]) {
            if (k < K) {
                ans_lst[q] = dist[x][y] + c * c + A[gx][gy];
            } else {
                long long cnt=dist[x][y]/w;
                long long d=dist[x][y]%w;
                ans_lst[q] = (cnt + 1) * c * c + d + A[gx][gy];
            }
        }
    }
    for (int i = 0; i < Q; ++i) {
        cout << ans_lst[i] << endl;
    }
    return 0;
}
0