結果
問題 | No.867 避難経路 |
ユーザー |
![]() |
提出日時 | 2019-08-16 22:22:22 |
言語 | C++14 (gcc 11.2.0 + boost 1.78.0) |
結果 |
AC
|
実行時間 | 3,514 ms / 6,000 ms |
コード長 | 2,063 bytes |
コンパイル時間 | 2,315 ms |
使用メモリ | 178,192 KB |
最終ジャッジ日時 | 2022-11-22 08:29:26 |
合計ジャッジ時間 | 91,497 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge11 |
テストケース
テストケース表示入力 | 結果 | 実行時間 使用メモリ |
---|---|---|
testcase_00 | AC | 4 ms
3,960 KB |
testcase_01 | AC | 4 ms
3,876 KB |
testcase_02 | AC | 3 ms
3,856 KB |
testcase_03 | AC | 3 ms
4,004 KB |
testcase_04 | AC | 3 ms
3,972 KB |
testcase_05 | AC | 3 ms
3,980 KB |
testcase_06 | AC | 4 ms
3,972 KB |
testcase_07 | AC | 3 ms
3,844 KB |
testcase_08 | AC | 3 ms
3,952 KB |
testcase_09 | AC | 4 ms
3,924 KB |
testcase_10 | AC | 3 ms
3,944 KB |
testcase_11 | AC | 3 ms
4,024 KB |
testcase_12 | AC | 4 ms
4,024 KB |
testcase_13 | AC | 3 ms
3,976 KB |
testcase_14 | AC | 3 ms
3,980 KB |
testcase_15 | AC | 3,314 ms
178,028 KB |
testcase_16 | AC | 3,221 ms
178,128 KB |
testcase_17 | AC | 3,514 ms
178,116 KB |
testcase_18 | AC | 3,253 ms
178,172 KB |
testcase_19 | AC | 3,241 ms
178,184 KB |
testcase_20 | AC | 3,094 ms
178,180 KB |
testcase_21 | AC | 3,063 ms
178,096 KB |
testcase_22 | AC | 3,187 ms
178,172 KB |
testcase_23 | AC | 3,303 ms
178,084 KB |
testcase_24 | AC | 3,104 ms
178,084 KB |
testcase_25 | AC | 3,123 ms
178,100 KB |
testcase_26 | AC | 3,174 ms
178,004 KB |
testcase_27 | AC | 3,193 ms
178,192 KB |
testcase_28 | AC | 3,194 ms
178,080 KB |
testcase_29 | AC | 3,326 ms
178,140 KB |
testcase_30 | AC | 2,772 ms
178,096 KB |
testcase_31 | AC | 2,741 ms
178,096 KB |
testcase_32 | AC | 2,801 ms
178,156 KB |
testcase_33 | AC | 2,756 ms
178,128 KB |
testcase_34 | AC | 2,832 ms
176,072 KB |
testcase_35 | AC | 2,972 ms
176,044 KB |
testcase_36 | AC | 2,957 ms
173,280 KB |
testcase_37 | AC | 2,752 ms
165,220 KB |
testcase_38 | AC | 2,740 ms
165,196 KB |
testcase_39 | AC | 2,792 ms
165,272 KB |
testcase_40 | AC | 2,750 ms
165,284 KB |
testcase_41 | AC | 2 ms
4,904 KB |
testcase_42 | AC | 2 ms
4,904 KB |
testcase_43 | AC | 3 ms
4,900 KB |
ソースコード
#include <bits/stdc++.h> int ri() { int n; scanf("%d", &n); return n; } std::pair<int, int> dirs[] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; int main() { int h = ri(), w = ri(); int gx = ri() - 1, gy = ri() - 1; int a[h][w]; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) a[i][j] = ri(); using T = std::pair<int64_t, std::pair<int, int> >; std::vector<std::vector<int64_t> > dist[350]; for (int i = 1; i < 350; i++) { int64_t cost_base = i * i; dist[i].resize(h, std::vector<int64_t>(w, 1000000000000000000)); std::priority_queue<T, std::vector<T>, std::greater<T> > que; que.push({a[gx][gy] + cost_base, {gx, gy}}); dist[i][gx][gy] = a[gx][gy] + cost_base; while (que.size()) { auto cur = que.top(); que.pop(); for (auto d : dirs) { int x = cur.second.first + d.first; int y = cur.second.second + d.second; if (x < 0 || x >= h || y < 0 || y >= w) continue; int64_t new_cost = cur.first + cost_base + a[x][y]; if (dist[i][x][y] > new_cost) { dist[i][x][y] = new_cost; que.push({new_cost, {x, y}}); } } } } std::vector<std::vector<int64_t> > normal_dist(h, std::vector<int64_t>(w, 1000000000000000000)); normal_dist[gx][gy] = a[gx][gy]; std::queue<std::pair<int, int> > que; que.push({gx, gy}); while (que.size()) { auto cur = que.front(); que.pop(); for (auto d : dirs) { int x = cur.first + d.first; int y = cur.second + d.second; if (x < 0 || x >= h || y < 0 || y >= w) continue; if (std::abs(cur.first - gx) + std::abs(cur.second - gy) >= std::abs(x - gx) + std::abs(y - gy)) continue; int64_t new_cost = normal_dist[cur.first][cur.second] + a[x][y]; if (normal_dist[x][y] > new_cost) { normal_dist[x][y] = new_cost; que.push({x, y}); } } } int q = ri(); for (int i = 0; i < q; i++) { int x = ri() - 1; int y = ri() - 1; int k = ri(); if (k < 350) { std::cout << dist[k][x][y] << std::endl; } else std::cout << normal_dist[x][y] + (int64_t) k * k * (std::abs(x - gx) + std::abs(y - gy) + 1) << std::endl; } return 0; }