結果

問題 No.867 避難経路
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-08-16 22:03:17
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 5,483 ms / 6,000 ms
コード長 3,987 bytes
コンパイル時間 2,328 ms
コンパイル使用メモリ 126,300 KB
実行使用メモリ 132,140 KB
最終ジャッジ日時 2023-09-04 02:25:01
合計ジャッジ時間 40,674 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
4,356 KB
testcase_01 AC 7 ms
4,376 KB
testcase_02 AC 8 ms
4,380 KB
testcase_03 AC 7 ms
4,380 KB
testcase_04 AC 7 ms
4,380 KB
testcase_05 AC 7 ms
4,376 KB
testcase_06 AC 7 ms
4,380 KB
testcase_07 AC 7 ms
4,380 KB
testcase_08 AC 7 ms
4,376 KB
testcase_09 AC 7 ms
4,376 KB
testcase_10 AC 8 ms
4,376 KB
testcase_11 AC 7 ms
4,376 KB
testcase_12 AC 7 ms
4,384 KB
testcase_13 AC 7 ms
4,380 KB
testcase_14 AC 8 ms
4,376 KB
testcase_15 AC 5,167 ms
129,460 KB
testcase_16 AC 5,125 ms
130,348 KB
testcase_17 AC 5,279 ms
129,156 KB
testcase_18 AC 5,089 ms
129,292 KB
testcase_19 AC 5,110 ms
130,220 KB
testcase_20 AC 5,078 ms
131,080 KB
testcase_21 AC 5,042 ms
130,008 KB
testcase_22 AC 5,104 ms
130,136 KB
testcase_23 AC 5,245 ms
132,140 KB
testcase_24 AC 5,110 ms
131,100 KB
testcase_25 AC 5,046 ms
129,580 KB
testcase_26 AC 5,116 ms
131,244 KB
testcase_27 AC 5,071 ms
130,852 KB
testcase_28 AC 5,224 ms
130,320 KB
testcase_29 AC 5,358 ms
129,692 KB
testcase_30 AC 4,803 ms
129,436 KB
testcase_31 AC 4,940 ms
131,148 KB
testcase_32 AC 4,843 ms
131,440 KB
testcase_33 AC 4,851 ms
131,504 KB
testcase_34 AC 4,810 ms
130,224 KB
testcase_35 AC 4,917 ms
130,948 KB
testcase_36 AC 4,893 ms
130,752 KB
testcase_37 AC 4,615 ms
125,356 KB
testcase_38 AC 4,645 ms
125,184 KB
testcase_39 AC 4,609 ms
126,604 KB
testcase_40 AC 5,483 ms
126,760 KB
testcase_41 AC 3 ms
4,384 KB
testcase_42 AC 5 ms
4,384 KB
testcase_43 AC 4 ms
4,384 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;

// sqrt(499 * 100)
enum L = 224;

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