結果

問題 No.867 避難経路
ユーザー finefine
提出日時 2019-09-06 00:40:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,878 bytes
コンパイル時間 3,539 ms
コンパイル使用メモリ 178,572 KB
実行使用メモリ 8,768 KB
最終ジャッジ日時 2023-09-05 03:40:25
合計ジャッジ時間 19,430 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,380 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,384 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 3 ms
4,384 KB
testcase_15 TLE -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
権限があれば一括ダウンロードができます

ソースコード

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]);
        calc(x, ny, dirx, diry, H, W);
    }
}

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];
    calc(gx, gy, 1, 1, H, W);
    calc(gx, gy, 1, -1, H, W);
    calc(gx, gy, -1, 1, H, W);
    calc(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