結果
問題 | No.2695 Warp Zone |
ユーザー | tenten |
提出日時 | 2024-04-11 16:01:22 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,147 ms / 2,000 ms |
コード長 | 3,338 bytes |
コンパイル時間 | 4,197 ms |
コンパイル使用メモリ | 85,600 KB |
実行使用メモリ | 114,336 KB |
最終ジャッジ日時 | 2024-10-02 21:38:44 |
合計ジャッジ時間 | 17,377 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 56 ms
50,008 KB |
testcase_01 | AC | 56 ms
50,396 KB |
testcase_02 | AC | 57 ms
50,384 KB |
testcase_03 | AC | 1,147 ms
114,336 KB |
testcase_04 | AC | 1,109 ms
113,084 KB |
testcase_05 | AC | 1,072 ms
112,908 KB |
testcase_06 | AC | 1,120 ms
113,104 KB |
testcase_07 | AC | 1,100 ms
114,284 KB |
testcase_08 | AC | 85 ms
50,912 KB |
testcase_09 | AC | 370 ms
75,860 KB |
testcase_10 | AC | 57 ms
50,172 KB |
testcase_11 | AC | 174 ms
57,184 KB |
testcase_12 | AC | 528 ms
83,148 KB |
testcase_13 | AC | 55 ms
50,080 KB |
testcase_14 | AC | 690 ms
102,924 KB |
testcase_15 | AC | 374 ms
69,760 KB |
testcase_16 | AC | 184 ms
57,204 KB |
testcase_17 | AC | 280 ms
63,004 KB |
testcase_18 | AC | 853 ms
110,004 KB |
testcase_19 | AC | 71 ms
50,744 KB |
testcase_20 | AC | 756 ms
105,276 KB |
testcase_21 | AC | 668 ms
102,568 KB |
testcase_22 | AC | 364 ms
66,764 KB |
testcase_23 | AC | 562 ms
82,872 KB |
testcase_24 | AC | 55 ms
50,268 KB |
testcase_25 | AC | 56 ms
50,312 KB |
testcase_26 | AC | 57 ms
50,180 KB |
ソースコード
import java.io.*; import java.util.*; import java.util.function.*; import java.util.stream.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int h = sc.nextInt(); int w = sc.nextInt(); int n = sc.nextInt(); Point[] points = new Point[n * 2 + 2]; points[0] = new Point(1, 1); points[1] = new Point(h, w); for (int i = 2; i < n * 2 + 2; i++) { points[i] = new Point(sc.nextInt(), sc.nextInt()); } List<List<Path>> graph = new ArrayList<>(); for (int i = 0; i < n * 2 + 2; i++) { graph.add(new ArrayList<>()); } for (int i = 1; i <= n; i++) { graph.get(i * 2).add(new Path(i * 2 + 1, 1)); for (int j = 1; j <= n; j++) { graph.get(i * 2 + 1).add(new Path(j * 2, points[i * 2 + 1].getDistance(points[j * 2]))); } graph.get(0).add(new Path(i * 2, points[0].getDistance(points[i * 2]))); graph.get(i * 2 + 1).add(new Path(1, points[1].getDistance(points[i * 2 + 1]))); } graph.get(0).add(new Path(1, points[0].getDistance(points[1]))); PriorityQueue<Path> queue = new PriorityQueue<>(); queue.add(new Path(0, 0)); long[] costs = new long[n * 2 + 2]; Arrays.fill(costs, Long.MAX_VALUE); while (queue.size() > 0) { Path p = queue.poll(); if (costs[p.idx] <= p.value) { continue; } costs[p.idx] = p.value; for (Path x : graph.get(p.idx)) { queue.add(new Path(x.idx, p.value + x.value)); } } System.out.println(costs[1]); } static class Path implements Comparable<Path> { int idx; long value; public Path(int idx, long value) { this.idx = idx; this.value = value; } public int compareTo(Path another) { return Long.compare(value, another.value); } public String toString() { return idx + ":" + value; } } static class Point { int x; int y; public Point(int x, int y) { this.x = x; this.y = y; } public int getDistance(Point p) { return Math.abs(x - p.x) + Math.abs(y - p.y); } } } class Scanner { BufferedReader br; StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); } catch (Exception e) { } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { try { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } catch (Exception e) { e.printStackTrace(); } finally { return st.nextToken(); } } }