結果
問題 |
No.867 避難経路
|
ユーザー |
|
提出日時 | 2019-08-16 23:49:52 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2,463 ms / 6,000 ms |
コード長 | 2,079 bytes |
コンパイル時間 | 1,848 ms |
コンパイル使用メモリ | 181,532 KB |
実行使用メモリ | 126,464 KB |
最終ジャッジ日時 | 2024-09-23 02:49:45 |
合計ジャッジ時間 | 65,886 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 41 |
ソースコード
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define repr(i, n) for (int i = (n) - 1; i >= 0; i--) using namespace std; using ll = long long; ll dp[250][250][250]; ll ep[250][250]; constexpr ll inf = 1e13; void chmin(ll &x, ll y) { x = min(x, y); } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int H, W, GY, GX; cin >> H >> W >> GY >> GX; GY--; GX--; vector<vector<int>> A(H, vector<int>(W)); rep(i, H) rep(j, W) cin >> A[i][j]; rep(k, 250) rep(i, 250) rep(j, 250) dp[k][i][j] = 1e18; dp[0][GY][GX] = A[GY][GX]; const int dy[] = {0, 1, 0, -1}; const int dx[] = {1, 0, -1, 0}; rep(k, 250) { dp[k][GY][GX] = A[GY][GX] + k*k; priority_queue<tuple<ll, int, int>> q; q.emplace(-dp[k][GY][GX], GY, GX); while (!q.empty()) { ll d; int y, x; tie(d, y, x) = q.top(); q.pop(); if (-d > dp[k][y][x]) continue; rep(l, 4) { int ny = y + dy[l]; int nx = x + dx[l]; if (0 <= ny && ny < H && 0 <= nx && nx < W) { if (dp[k][ny][nx] > dp[k][y][x] + A[ny][nx] + k*k) { dp[k][ny][nx] = dp[k][y][x] + A[ny][nx] + k*k; q.emplace(-dp[k][ny][nx], ny, nx); } } } } } rep(i, 250) rep(j, 250) ep[i][j] = 1e18; ep[GY][GX] = A[GY][GX] + inf; priority_queue<tuple<ll, int, int>> q; q.emplace(-ep[GY][GX], GY, GX); while (!q.empty()) { ll d; int y, x; tie(d, y, x) = q.top(); q.pop(); if (-d > ep[y][x]) continue; rep(l, 4) { int ny = y + dy[l]; int nx = x + dx[l]; if (0 <= ny && ny < H && 0 <= nx && nx < W) { if (ep[ny][nx] > ep[y][x] + A[ny][nx] + inf) { ep[ny][nx] = ep[y][x] + A[ny][nx] + inf; q.emplace(-ep[ny][nx], ny, nx); } } } } int Q; cin >> Q; while (Q--) { int y, x; ll k; cin >> y >> x >> k; y--; x--; if (k < 250) { cout << dp[k][y][x] << '\n'; } else { ll d = (abs(GY - y) + abs(GX - x) + 1); cout << ep[y][x] - d * inf + d * k * k << '\n'; } } }