結果

問題 No.867 避難経路
ユーザー mencotton
提出日時 2019-08-17 00:03:01
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,593 ms / 6,000 ms
コード長 1,724 bytes
コンパイル時間 1,414 ms
コンパイル使用メモリ 95,804 KB
実行使用メモリ 22,656 KB
最終ジャッジ日時 2024-09-23 04:23:54
合計ジャッジ時間 45,477 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

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