結果

問題 No.867 避難経路
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-08-16 21:58:23
言語 D
(dmd 2.109.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
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

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) {
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0