結果
| 問題 |
No.867 避難経路
|
| コンテスト | |
| ユーザー |
risujiroh
|
| 提出日時 | 2019-08-16 22:50:46 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,439 bytes |
| コンパイル時間 | 2,190 ms |
| コンパイル使用メモリ | 189,776 KB |
| 実行使用メモリ | 29,312 KB |
| 最終ジャッジ日時 | 2024-09-22 19:32:50 |
| 合計ジャッジ時間 | 30,547 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 3 |
| other | AC * 22 WA * 8 TLE * 1 -- * 10 |
ソースコード
#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> >;
struct Edge { int to, 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, 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 (k >= 2500) {
for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) {
g[in(i, j)][0].cost = a[i][j] + 2500 * 2500;
}
auto d = dijkstra<lint>(g, in(gi, gj));
while (itr != end(qs)) {
res[itr->t] = d[out(itr->i, itr->j)] + (abs(itr->i - gi) + abs(itr->j - gj) + 1) * (k * k - 2500 * 2500);
++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