結果
問題 | No.867 避難経路 |
ユーザー | 👑 hos.lyric |
提出日時 | 2019-08-16 21:58:23 |
言語 | D (dmd 2.106.1) |
結果 |
AC
|
実行時間 | 5,481 ms / 6,000 ms |
コード長 | 3,981 bytes |
コンパイル時間 | 1,221 ms |
コンパイル使用メモリ | 141,120 KB |
実行使用メモリ | 150,016 KB |
最終ジャッジ日時 | 2024-06-22 02:15:59 |
合計ジャッジ時間 | 145,621 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 7 ms
6,812 KB |
testcase_01 | AC | 8 ms
6,940 KB |
testcase_02 | AC | 8 ms
6,940 KB |
testcase_03 | AC | 8 ms
6,940 KB |
testcase_04 | AC | 8 ms
6,940 KB |
testcase_05 | AC | 8 ms
6,940 KB |
testcase_06 | AC | 8 ms
6,944 KB |
testcase_07 | AC | 8 ms
6,940 KB |
testcase_08 | AC | 8 ms
6,940 KB |
testcase_09 | AC | 8 ms
6,940 KB |
testcase_10 | AC | 8 ms
6,940 KB |
testcase_11 | AC | 8 ms
6,944 KB |
testcase_12 | AC | 8 ms
6,940 KB |
testcase_13 | AC | 9 ms
6,940 KB |
testcase_14 | AC | 8 ms
6,944 KB |
testcase_15 | AC | 5,351 ms
149,328 KB |
testcase_16 | AC | 5,255 ms
148,216 KB |
testcase_17 | AC | 5,470 ms
149,632 KB |
testcase_18 | AC | 5,212 ms
150,016 KB |
testcase_19 | AC | 5,225 ms
148,608 KB |
testcase_20 | AC | 5,308 ms
148,916 KB |
testcase_21 | AC | 5,113 ms
149,384 KB |
testcase_22 | AC | 5,275 ms
148,836 KB |
testcase_23 | AC | 5,460 ms
148,572 KB |
testcase_24 | AC | 5,190 ms
148,552 KB |
testcase_25 | AC | 5,192 ms
148,480 KB |
testcase_26 | AC | 5,203 ms
148,916 KB |
testcase_27 | AC | 5,178 ms
149,676 KB |
testcase_28 | AC | 5,393 ms
148,812 KB |
testcase_29 | AC | 5,481 ms
148,432 KB |
testcase_30 | AC | 4,914 ms
149,108 KB |
testcase_31 | AC | 5,047 ms
148,292 KB |
testcase_32 | AC | 4,950 ms
148,364 KB |
testcase_33 | AC | 4,910 ms
148,272 KB |
testcase_34 | AC | 4,941 ms
148,384 KB |
testcase_35 | AC | 5,060 ms
148,312 KB |
testcase_36 | AC | 4,946 ms
147,612 KB |
testcase_37 | AC | 4,645 ms
144,068 KB |
testcase_38 | AC | 4,667 ms
143,580 KB |
testcase_39 | AC | 4,645 ms
144,036 KB |
testcase_40 | AC | 4,664 ms
144,028 KB |
testcase_41 | AC | 2 ms
6,940 KB |
testcase_42 | AC | 4 ms
6,940 KB |
testcase_43 | AC | 4 ms
6,940 KB |
ソースコード
import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } enum INF = 10L^^18; enum L = 260; enum M = 510; int H, W; int GX, GY; long[][] A; int Q; int[] X, Y; long[] K; void main() { try { for (; ; ) { H = readInt(); W = readInt(); GX = readInt() - 1; GY = readInt() - 1; A = new long[][](H, W); foreach (x; 0 .. H) foreach (y; 0 .. W) { A[x][y] = readLong(); } Q = readInt(); X = new int[Q]; Y = new int[Q]; K = new long[Q]; foreach (q; 0 .. Q) { X[q] = readInt() - 1; Y[q] = readInt() - 1; K[q] = readLong(); } // small auto dist = new long[][][](L, H, W); foreach (k; 0 .. L) { const k2 = 1L * k * k; alias Node = Tuple!(long, "c", int, "x", int, "y"); BinaryHeap!(Array!Node, "a > b") q; foreach (x; 0 .. H) foreach (y; 0 .. W) { dist[k][x][y] = INF; } dist[k][GX][GY] = k2 + A[GX][GY]; q.insert(Node(dist[k][GX][GY], GX, GY)); for (; !q.empty; ) { const c = q.front.c; const x = q.front.x; const y = q.front.y; q.removeFront; if (dist[k][x][y] == c) { void update(int xx, int yy) { const cc = c + k2 + A[xx][yy]; if (chmin(dist[k][xx][yy], cc)) { q.insert(Node(cc, 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 auto dp = new long[][](H, W); foreach (x; 0 .. H) foreach (y; 0 .. W) { dp[x][y] = INF; } dp[GX][GY] = A[GX][GY]; foreach (x; GX .. H) foreach (y; GY .. W) { 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]); } foreach (x; GX .. H) foreach_reverse (y; 0 .. GY + 1) { 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]); } foreach_reverse (x; 0 .. GX + 1) foreach (y; GY .. W) { 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]); } foreach_reverse (x; 0 .. GX + 1) foreach_reverse (y; 0 .. GY + 1) { 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]); } foreach (q; 0 .. Q) { long ans; if (K[q] < L) { ans = dist[cast(int)(K[q])][X[q]][Y[q]]; } else { ans = 1L * K[q] * K[q] * (1 + abs(X[q] - GX) + abs(Y[q] - GY)) + dp[X[q]][Y[q]]; } writeln(ans); } } } catch (EOFException e) { } }