結果

問題 No.867 避難経路
ユーザー ks2mks2m
提出日時 2019-08-16 23:05:18
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,524 bytes
コンパイル時間 5,442 ms
コンパイル使用メモリ 79,252 KB
実行使用メモリ 37,140 KB
最終ジャッジ日時 2023-10-24 04:09:03
合計ジャッジ時間 18,437 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
37,116 KB
testcase_01 AC 54 ms
37,116 KB
testcase_02 AC 54 ms
37,120 KB
testcase_03 AC 55 ms
37,100 KB
testcase_04 AC 55 ms
37,120 KB
testcase_05 AC 54 ms
37,132 KB
testcase_06 AC 53 ms
37,100 KB
testcase_07 AC 55 ms
37,140 KB
testcase_08 AC 56 ms
37,112 KB
testcase_09 AC 55 ms
37,100 KB
testcase_10 AC 55 ms
37,116 KB
testcase_11 AC 54 ms
37,112 KB
testcase_12 AC 57 ms
37,140 KB
testcase_13 AC 57 ms
37,088 KB
testcase_14 AC 55 ms
37,100 KB
testcase_15 TLE -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		int h = Integer.parseInt(sa[0]);
		int w = Integer.parseInt(sa[1]);
		sa = br.readLine().split(" ");
		int gx = Integer.parseInt(sa[0]) - 1;
		int gy = Integer.parseInt(sa[1]) - 1;
		int[][] a = new int[h][w];
		for (int i = 0; i < h; i++) {
			sa = br.readLine().split(" ");
			for (int j = 0; j < w; j++) {
				a[i][j] = Integer.parseInt(sa[j]);
			}
		}
		int q = Integer.parseInt(br.readLine());
		int[] x = new int[q];
		int[] y = new int[q];
		long[] k = new long[q];
		for (int i = 0; i < q; i++) {
			sa = br.readLine().split(" ");
			x[i] = Integer.parseInt(sa[0]) - 1;
			y[i] = Integer.parseInt(sa[1]) - 1;
			k[i] = Integer.parseInt(sa[2]);
			k[i] *= k[i];
		}
		br.close();

		int[] dx = {-1, 1, 0, 0};
		int[] dy = {0, 0, -1, 1};
		int OFFSET = 1000;

		int[][] d = new int[h][w];
		Map<Integer, List<Masu>> map = new HashMap<>();
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				d[i][j] = Integer.MAX_VALUE;
				List<Masu> list = new ArrayList<>();
				map.put(i + j * 1000, list);
			}
		}
		Queue<Masu> que = new ArrayDeque<Masu>();
		Masu first = new Masu();
		first.x = gx;
		first.y = gy;
		first.t = 1;
		first.d = a[gx][gy];
		que.add(first);
		d[gx][gy] = first.d;
		map.get(gx + gy * OFFSET).add(first);
		while (!que.isEmpty()) {
			Masu cur = que.poll();
			for (int i = 0; i < 4; i++) {
				int nx = cur.x + dx[i];
				if (nx < 0 || h <= nx) {
					continue;
				}
				int ny = cur.y + dy[i];
				if (ny < 0 || w <= ny) {
					continue;
				}
				int nd = cur.d + a[nx][ny];
				if (nd < d[nx][ny]) {
					Masu next = new Masu();
					next.x = nx;
					next.y = ny;
					next.t = cur.t + 1;
					next.d = nd;
					que.add(next);
					d[nx][ny] = nd;
					map.get(nx + ny * OFFSET).add(next);
				}
			}
		}

		PrintWriter pw = new PrintWriter(System.out);
		for (int i = 0; i < q; i++) {
			long ans = Long.MAX_VALUE;
			for (Masu masu : map.get(x[i] + y[i] * OFFSET)) {
				long val = k[i] * masu.t + masu.d;
				ans = Math.min(ans, val);
			}
			pw.println(ans);
		}
		pw.flush();
	}

	static class Masu {
		int x, y, t, d;
	}
}
0