結果
| 問題 |
No.867 避難経路
|
| コンテスト | |
| ユーザー |
risujiroh
|
| 提出日時 | 2019-08-16 23:08:12 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 5,076 ms / 6,000 ms |
| コード長 | 2,690 bytes |
| コンパイル時間 | 2,352 ms |
| コンパイル使用メモリ | 191,580 KB |
| 実行使用メモリ | 29,996 KB |
| 最終ジャッジ日時 | 2024-09-22 21:48:48 |
| 合計ジャッジ時間 | 105,948 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 41 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;
using namespace chrono;
auto start_t = steady_clock::now();
void start() { start_t = steady_clock::now(); }
int elapsed_time() { return duration_cast<milliseconds>(steady_clock::now() - start_t).count(); }
struct Edge { int to; lint cost; };
template<class T, class Edge> V<T> dijkstra(const VV<Edge>& g, int s = 0) {
V<T> dist(g.size(), numeric_limits<T>::max());
using P = pair<T, int>;
priority_queue< P, V<P>, greater<P> > pque;
pque.emplace(dist[s] = 0, s);
while (!pque.empty()) {
T d; int v;
tie(d, v) = pque.top(); pque.pop();
if (d > dist[v]) continue;
for (const auto& e : g[v]) if (dist[v] + e.cost < dist[e.to]) {
pque.emplace(dist[e.to] = dist[v] + e.cost, e.to);
}
}
return dist;
}
int main() {
cin.tie(nullptr); ios::sync_with_stdio(false);
int h, w; cin >> h >> w;
int gi, gj; cin >> gi >> gj, --gi, --gj;
auto in = [&](int i, int j) -> int { return i * w + j; };
auto out = [&](int i, int j) -> int { return (h + i) * w + j; };
VV<Edge> g(2 * h * w);
VV<> a(h, V<>(w));
for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) {
cin >> a[i][j];
g[in(i, j)].emplace_back(Edge{out(i, j), a[i][j]});
for (int di = -1; di <= 1; ++di) for (int dj = -1; dj <= 1; ++dj) if (abs(di) + abs(dj) == 1) {
int ni = i + di, nj = j + dj;
if (ni < 0 or ni >= h or nj < 0 or nj >= w) continue;
g[out(i, j)].emplace_back(Edge{in(ni, nj), 0});
}
}
int q; cin >> q;
struct Q { int t, i, j; lint k; };
V<Q> qs(q);
for (int t = 0; t < q; ++t) {
qs[t].t = t;
cin >> qs[t].i >> qs[t].j >> qs[t].k, --qs[t].i, --qs[t].j;
}
sort(begin(qs), end(qs), [](const auto& l, const auto& r) { return l.k < r.k; });
V<lint> res(q);
auto itr = begin(qs);
while (itr != end(qs)) {
lint k = itr->k;
if (elapsed_time() >= 5000) {
for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) {
g[in(i, j)][0].cost = a[i][j] + k * k;
}
auto d = dijkstra<lint>(g, in(gi, gj));
while (itr != end(qs)) {
int dist = abs(itr->i - gi) + abs(itr->j - gj) + 1;
res[itr->t] = d[out(itr->i, itr->j)] + dist * (itr->k * itr->k - k * k);
++itr;
}
break;
}
for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) {
g[in(i, j)][0].cost = a[i][j] + k * k;
}
auto d = dijkstra<lint>(g, in(gi, gj));
while (itr != end(qs) and itr->k == k) {
res[itr->t] = d[out(itr->i, itr->j)];
++itr;
}
}
for (lint e : res) cout << e << '\n';
}
risujiroh