結果
| 問題 |
No.867 避難経路
|
| コンテスト | |
| ユーザー |
ks2m
|
| 提出日時 | 2019-08-16 23:23:13 |
| 言語 | Java (openjdk 23) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 2,465 bytes |
| コンパイル時間 | 2,513 ms |
| コンパイル使用メモリ | 78,920 KB |
| 実行使用メモリ | 753,780 KB |
| 最終ジャッジ日時 | 2024-09-22 22:21:40 |
| 合計ジャッジ時間 | 14,525 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 3 |
| other | AC * 15 MLE * 1 -- * 25 |
ソースコード
import java.io.BufferedReader;
import java.io.InputStreamReader;
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(" ");
short gx = Short.parseShort(sa[0]);
gx--;
short gy = Short.parseShort(sa[1]);
gy--;
short[][] a = new short[h][w];
for (int i = 0; i < h; i++) {
sa = br.readLine().split(" ");
for (int j = 0; j < w; j++) {
a[i][j] = Short.parseShort(sa[j]);
}
}
short[] dx = {-1, 1, 0, 0};
short[] 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<>(1);
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++) {
short nx = (short) (cur.x + dx[i]);
if (nx < 0 || h <= nx) {
continue;
}
short ny = (short) (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);
}
}
}
int q = Integer.parseInt(br.readLine());
int x, y, k;
// PrintWriter pw = new PrintWriter(System.out);
for (int i = 0; i < q; i++) {
sa = br.readLine().split(" ");
x = Integer.parseInt(sa[0]) - 1;
y = Integer.parseInt(sa[1]) - 1;
k = Integer.parseInt(sa[2]);
k *= k;
long ans = Long.MAX_VALUE;
for (Masu masu : map.get(x + y * OFFSET)) {
long val = k * masu.t + masu.d;
ans = Math.min(ans, val);
}
// pw.println(ans);
System.out.println(ans);
}
// pw.flush();
br.close();
}
static class Masu {
short x, y;
int t, d;
}
}
ks2m