結果

問題 No.867 避難経路
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-08-16 21:58:23
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 5,957 ms / 6,000 ms
コード長 3,981 bytes
コンパイル時間 2,681 ms
コンパイル使用メモリ 125,608 KB
実行使用メモリ 149,356 KB
最終ジャッジ日時 2023-09-04 02:18:59
合計ジャッジ時間 160,852 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
4,376 KB
testcase_01 AC 9 ms
4,376 KB
testcase_02 AC 8 ms
4,380 KB
testcase_03 AC 8 ms
4,380 KB
testcase_04 AC 8 ms
4,376 KB
testcase_05 AC 9 ms
4,376 KB
testcase_06 AC 8 ms
4,380 KB
testcase_07 AC 9 ms
4,376 KB
testcase_08 AC 8 ms
4,380 KB
testcase_09 AC 8 ms
4,376 KB
testcase_10 AC 8 ms
4,380 KB
testcase_11 AC 8 ms
4,376 KB
testcase_12 AC 8 ms
4,380 KB
testcase_13 AC 8 ms
4,376 KB
testcase_14 AC 9 ms
4,380 KB
testcase_15 AC 5,836 ms
148,452 KB
testcase_16 AC 5,731 ms
148,108 KB
testcase_17 AC 5,908 ms
149,160 KB
testcase_18 AC 5,729 ms
148,840 KB
testcase_19 AC 5,751 ms
149,264 KB
testcase_20 AC 5,740 ms
148,256 KB
testcase_21 AC 5,634 ms
148,760 KB
testcase_22 AC 5,726 ms
147,892 KB
testcase_23 AC 5,898 ms
147,988 KB
testcase_24 AC 5,693 ms
147,796 KB
testcase_25 AC 5,715 ms
147,976 KB
testcase_26 AC 5,784 ms
148,028 KB
testcase_27 AC 5,729 ms
147,872 KB
testcase_28 AC 5,903 ms
148,988 KB
testcase_29 AC 5,957 ms
148,180 KB
testcase_30 AC 5,446 ms
148,244 KB
testcase_31 AC 5,505 ms
148,156 KB
testcase_32 AC 5,532 ms
147,900 KB
testcase_33 AC 5,445 ms
147,896 KB
testcase_34 AC 5,400 ms
147,464 KB
testcase_35 AC 5,527 ms
149,356 KB
testcase_36 AC 5,526 ms
147,052 KB
testcase_37 AC 5,180 ms
144,140 KB
testcase_38 AC 5,169 ms
144,224 KB
testcase_39 AC 5,186 ms
143,192 KB
testcase_40 AC 5,126 ms
143,296 KB
testcase_41 AC 3 ms
4,384 KB
testcase_42 AC 4 ms
4,396 KB
testcase_43 AC 5 ms
4,388 KB
権限があれば一括ダウンロードができます

ソースコード

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