結果

問題 No.867 避難経路
ユーザー finefine
提出日時 2019-09-06 00:57:06
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2,039 ms / 6,000 ms
コード長 3,000 bytes
コンパイル時間 4,139 ms
コンパイル使用メモリ 175,972 KB
実行使用メモリ 103,332 KB
最終ジャッジ日時 2023-09-05 04:47:05
合計ジャッジ時間 59,138 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,384 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 3 ms
4,388 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 3 ms
4,384 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 3 ms
4,384 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 3 ms
4,384 KB
testcase_12 AC 3 ms
4,384 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 1,898 ms
103,208 KB
testcase_16 AC 1,821 ms
103,212 KB
testcase_17 AC 2,039 ms
103,268 KB
testcase_18 AC 1,802 ms
103,208 KB
testcase_19 AC 1,800 ms
103,208 KB
testcase_20 AC 1,744 ms
103,332 KB
testcase_21 AC 1,695 ms
103,208 KB
testcase_22 AC 1,806 ms
103,300 KB
testcase_23 AC 1,976 ms
103,228 KB
testcase_24 AC 1,698 ms
103,316 KB
testcase_25 AC 1,734 ms
103,272 KB
testcase_26 AC 1,754 ms
103,324 KB
testcase_27 AC 1,808 ms
103,216 KB
testcase_28 AC 1,697 ms
103,260 KB
testcase_29 AC 1,752 ms
103,272 KB
testcase_30 AC 1,505 ms
103,024 KB
testcase_31 AC 1,580 ms
103,228 KB
testcase_32 AC 1,570 ms
102,940 KB
testcase_33 AC 1,552 ms
103,016 KB
testcase_34 AC 1,514 ms
101,932 KB
testcase_35 AC 1,568 ms
101,800 KB
testcase_36 AC 1,540 ms
100,364 KB
testcase_37 AC 1,458 ms
95,852 KB
testcase_38 AC 1,468 ms
95,932 KB
testcase_39 AC 1,484 ms
95,856 KB
testcase_40 AC 1,488 ms
95,984 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 3 ms
4,384 KB
testcase_43 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};

const ll INF = 1e16;

ll a[251][251];

struct State {
    int x;
    int y;
    ll cost;
    //int prev;
    State(int x, int y, ll cost/*, int prev*/) : x(x), y(y), cost(cost)/*, prev(prev)*/ {}
    bool operator>(const State& s) const {
        //if (cost != s.cost) return cost > s.cost;
        //if (prev != s.prev) return prev > s.prev; //最短経路を辞書順最小にする(省略可)
        //return at > s.at;
        return cost > s.cost;
    }
};

//const int NONE = -1;

//sは始点、mincは最短経路のコスト、Prevは最短経路をたどる際の前の頂点
void dijkstra(int sx, int sy, ll k, int H, int W, vector< vector<ll> >& minc/*, vector<int>& Prev*/){
    minc.assign(H, vector<ll>(W, INF));
    //Prev.assign(graph.size(), INF);
    priority_queue<State, vector<State>, greater<State> > pq;
    pq.emplace(sx, sy, k + a[sx][sy]/*, NONE*/);
    while(!pq.empty()) {
        State cur = pq.top();
        pq.pop();
        if (minc[cur.x][cur.y] <= cur.cost) continue;
        minc[cur.x][cur.y] = cur.cost;
        //Prev[cur.at] = cur.prev;
        for (int i = 0; i < 4; i++) {
            int nx = cur.x + dx[i], ny = cur.y + dy[i];
            if (nx < 0 || nx >= H || ny < 0 || ny >= W) continue;
            ll cost = cur.cost + a[nx][ny] + k;
            if (minc[nx][ny] <= cost) continue;
            pq.emplace(nx, ny, cost);
        }
    }
}

ll dp[251][251];

void calc(int x, int y, int dirx, int diry, int H, int W) {
    int nx = x + dirx;
    if (nx >= 0 && nx < H) {
        dp[nx][y] = min(dp[nx][y], dp[x][y] + a[nx][y]);
        calc(nx, y, dirx, diry, H, W);
    }

    int ny = y + diry;
    if (ny >= 0 && ny < W) {
        dp[x][ny] = min(dp[x][ny], dp[x][y] + a[x][ny]);
    }
}

void solve(int x, int y, int dirx, int diry, int H, int W) {
    while (y >= 0 && y < W) {
        calc(x, y, dirx, diry, H, W);
        y += diry;
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int H, W, gx, gy;
    cin >> H >> W >> gx >> gy;
    gx--; gy--;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cin >> a[i][j];
            dp[i][j] = INF;
        }
    }

    dp[gx][gy] = a[gx][gy];
    solve(gx, gy, 1, 1, H, W);
    solve(gx, gy, 1, -1, H, W);
    solve(gx, gy, -1, 1, H, W);
    solve(gx, gy, -1, -1, H, W);

    constexpr ll K_SMALL = 200;
    vector< vector< vector<ll> > > mincs(K_SMALL);
    for (ll k = 1; k < K_SMALL; k++) {
        dijkstra(gx, gy, k * k, H, W, mincs[k]);
    }

    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int x, y;
        ll k;
        cin >> x >> y >> k;
        x--; y--;
        if (k >= K_SMALL) {
            k *= k;
            cout << k * (abs(x - gx) + abs(y - gy) + 1) + dp[x][y] << "\n";
        } else {
            cout << mincs[k][x][y] << "\n";
        }
    }
    return 0;
}
0