結果

問題 No.2695 Warp Zone
ユーザー tentententen
提出日時 2024-04-11 16:01:22
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,071 ms / 2,000 ms
コード長 3,338 bytes
コンパイル時間 7,057 ms
コンパイル使用メモリ 89,212 KB
実行使用メモリ 116,212 KB
最終ジャッジ日時 2024-04-11 16:01:44
合計ジャッジ時間 20,065 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
50,228 KB
testcase_01 AC 54 ms
50,304 KB
testcase_02 AC 55 ms
50,416 KB
testcase_03 AC 1,071 ms
114,124 KB
testcase_04 AC 1,018 ms
113,100 KB
testcase_05 AC 1,031 ms
113,264 KB
testcase_06 AC 1,050 ms
112,820 KB
testcase_07 AC 1,031 ms
116,212 KB
testcase_08 AC 70 ms
50,752 KB
testcase_09 AC 367 ms
75,904 KB
testcase_10 AC 55 ms
50,384 KB
testcase_11 AC 166 ms
57,156 KB
testcase_12 AC 510 ms
83,084 KB
testcase_13 AC 56 ms
50,156 KB
testcase_14 AC 646 ms
102,736 KB
testcase_15 AC 389 ms
70,068 KB
testcase_16 AC 180 ms
57,216 KB
testcase_17 AC 274 ms
62,916 KB
testcase_18 AC 821 ms
109,840 KB
testcase_19 AC 78 ms
50,840 KB
testcase_20 AC 702 ms
105,352 KB
testcase_21 AC 620 ms
102,748 KB
testcase_22 AC 359 ms
66,836 KB
testcase_23 AC 549 ms
83,200 KB
testcase_24 AC 53 ms
49,984 KB
testcase_25 AC 52 ms
50,120 KB
testcase_26 AC 54 ms
50,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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();
        }
    }
    
}
0