結果
問題 |
No.2695 Warp Zone
|
ユーザー |
![]() |
提出日時 | 2024-04-11 16:01:22 |
言語 | Java (openjdk 23) |
結果 |
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 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 24 |
ソースコード
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(); } } }