結果
| 問題 |
No.867 避難経路
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-08-16 22:09:48 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2,119 ms / 6,000 ms |
| コード長 | 3,694 bytes |
| コンパイル時間 | 2,283 ms |
| コンパイル使用メモリ | 128,852 KB |
| 実行使用メモリ | 129,952 KB |
| 最終ジャッジ日時 | 2024-09-22 17:22:54 |
| 合計ジャッジ時間 | 63,557 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 41 |
ソースコード
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target ("avx")
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> void chmin(T &t, const T &f) { if (t > f) t = f; }
template <class T> void chmax(T &t, const T &f) { if (t < f) t = f; }
constexpr Int INF = 1001001001001001001LL;
constexpr int MAX = 260;
constexpr int L = 224;
int H, W;
int GX, GY;
Int A[MAX][MAX];
int Q;
int X[500010], Y[500010];
Int K[500010];
Int dist[L + 10][MAX][MAX];
Int dp[MAX][MAX];
int main() {
for (; ~scanf("%d%d", &H, &W); ) {
scanf("%d%d", &GX, &GY);
--GX;
--GY;
for (int x = 0; x < H; ++x) for (int y = 0; y < W; ++y) {
scanf("%lld", &A[x][y]);
}
scanf("%d", &Q);
for (int q = 0; q < Q; ++q) {
scanf("%d%d%lld", &X[q], &Y[q], &K[q]);
--X[q];
--Y[q];
}
// small
for (int k = 0; k < L; ++k) {
const Int k2 = 1LL * k * k;
using Node = pair<Int, pair<int, int>>;
priority_queue<Node, vector<Node>, greater<Node>> q;
for (int x = 0; x < H; ++x) for (int y = 0; y < W; ++y) {
dist[k][x][y] = INF;
}
dist[k][GX][GY] = k2 + A[GX][GY];
q.emplace(dist[k][GX][GY], make_pair(GX, GY));
for (; !q.empty(); ) {
const Int c = q.top().first;
const int x = q.top().second.first;
const int y = q.top().second.second;
q.pop();
if (dist[k][x][y] == c) {
auto update = [&](int xx, int yy) {
const Int cc = c + k2 + A[xx][yy];
if (dist[k][xx][yy] > cc) {
dist[k][xx][yy] = cc;
q.emplace(cc, make_pair(xx, yy));
}
};
if (x + 1 < H) update(x + 1, y);
if (y + 1 < W) update(x, y + 1);
if (x - 1 >= 0) update(x - 1, y);
if (y - 1 >= 0) update(x, y - 1);
}
}
}
// large
for (int x = 0; x < H; ++x) for (int y = 0; y < W; ++y) {
dp[x][y] = INF;
}
dp[GX][GY] = A[GX][GY];
for (int x = GX; x < H; ++x) for (int y = GY; y < W; ++y) {
if (x > GX) chmin(dp[x][y], dp[x - 1][y] + A[x][y]);
if (y > GY) chmin(dp[x][y], dp[x][y - 1] + A[x][y]);
}
for (int x = GX; x < H; ++x) for (int y = GY; y >= 0; --y) {
if (x > GX) chmin(dp[x][y], dp[x - 1][y] + A[x][y]);
if (y < GY) chmin(dp[x][y], dp[x][y + 1] + A[x][y]);
}
for (int x = GX; x >= 0; --x) for (int y = GY; y < W; ++y) {
if (x < GX) chmin(dp[x][y], dp[x + 1][y] + A[x][y]);
if (y > GY) chmin(dp[x][y], dp[x][y - 1] + A[x][y]);
}
for (int x = GX; x >= 0; --x) for (int y = GY; y >= 0; --y) {
if (x < GX) chmin(dp[x][y], dp[x + 1][y] + A[x][y]);
if (y < GY) chmin(dp[x][y], dp[x][y + 1] + A[x][y]);
}
for (int q = 0; q < Q; ++q) {
Int ans;
if (K[q] < L) {
ans = dist[K[q]][X[q]][Y[q]];
} else {
ans = 1LL * K[q] * K[q] * (1 + abs(X[q] - GX) + abs(Y[q] - GY)) + dp[X[q]][Y[q]];
}
printf("%lld\n", ans);
}
}
return 0;
}