結果

問題 No.867 避難経路
ユーザー mencottonmencotton
提出日時 2019-08-17 00:03:01
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,541 ms / 6,000 ms
コード長 1,724 bytes
コンパイル時間 1,121 ms
コンパイル使用メモリ 95,952 KB
実行使用メモリ 22,756 KB
最終ジャッジ日時 2023-10-24 12:04:48
合計ジャッジ時間 43,324 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 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 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 1,319 ms
14,912 KB
testcase_16 AC 1,457 ms
18,384 KB
testcase_17 AC 1,308 ms
13,800 KB
testcase_18 AC 1,432 ms
17,948 KB
testcase_19 AC 1,467 ms
18,220 KB
testcase_20 AC 1,398 ms
17,700 KB
testcase_21 AC 1,488 ms
20,004 KB
testcase_22 AC 1,480 ms
17,248 KB
testcase_23 AC 1,347 ms
14,384 KB
testcase_24 AC 1,524 ms
18,520 KB
testcase_25 AC 1,541 ms
18,616 KB
testcase_26 AC 1,536 ms
18,832 KB
testcase_27 AC 1,423 ms
15,656 KB
testcase_28 AC 1,190 ms
8,348 KB
testcase_29 AC 1,168 ms
8,460 KB
testcase_30 AC 1,106 ms
9,092 KB
testcase_31 AC 1,072 ms
9,356 KB
testcase_32 AC 1,070 ms
9,356 KB
testcase_33 AC 1,064 ms
9,356 KB
testcase_34 AC 1,136 ms
9,356 KB
testcase_35 AC 1,463 ms
22,756 KB
testcase_36 AC 1,085 ms
9,356 KB
testcase_37 AC 1,399 ms
18,768 KB
testcase_38 AC 1,330 ms
17,012 KB
testcase_39 AC 1,282 ms
14,796 KB
testcase_40 AC 1,237 ms
13,580 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 <iostream>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;
typedef long long ll;

int h, w;
vector<vector<int>> ban;
vector<vector<vector<pair<ll, ll>>>> dp;

void bfs(vector<ll> start) {
    priority_queue<vector<ll>, vector<vector<ll>>, greater<>> que;
    que.push(start);

    vector<int> mx{1, -1, 0, 0};
    vector<int> my{0, 0, 1, -1};
    while (true) {
        if (que.empty())break;

        vector<ll> now = que.top();
        que.pop();
        auto &v = dp[now[2]][now[3]];
        if (now[1] >= v[v.size() - 1].second)continue;

        v.push_back({now[0], now[1]});
        for (int i = 0; i < 4; i++) {
            int ax = now[2] + mx[i];
            int ay = now[3] + my[i];
            if (0 <= ax && ax < h && 0 <= ay && ay < w) {
                auto nv = dp[ax][ay];
                if (now[1] + ban[ax][ay] < nv[nv.size() - 1].second)
                    que.push({now[0] + 1, now[1] + ban[ax][ay], ax, ay});
            }
        }
    }
}

int main() {
    int gx, gy;
    cin >> h >> w >> gx >> gy;
    gx--;
    gy--;
    ban = vector<vector<int>>(h, vector<int>(w));
    for (int i = 0; i < h; i++)
        for (int j = 0; j < w; j++)cin >> ban[i][j];

    dp = vector<vector<vector<pair<ll, ll>>>>
            (h, vector<vector<pair<ll, ll>>>(w, vector<pair<ll, ll>>{{0, 1145141919810364}}));
    bfs({1, ban[gx][gy], gx, gy});

    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        ll x, y, k;
        cin >> x >> y >> k;
        x--;
        y--;
        ll ret = 1145141919810364364;
        for (auto usi:dp[x][y]) {
            ret = min(ret, k * k * usi.first + usi.second);
        }

        cout << ret << endl;
    }
    return 0;
}
0