結果

問題 No.614 壊れたキャンパス
ユーザー tentententen
提出日時 2021-03-09 20:11:06
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,752 bytes
コンパイル時間 2,839 ms
コンパイル使用メモリ 80,376 KB
実行使用メモリ 148,668 KB
最終ジャッジ日時 2024-04-19 21:15:24
合計ジャッジ時間 9,219 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
46,504 KB
testcase_01 AC 128 ms
41,492 KB
testcase_02 AC 127 ms
40,772 KB
testcase_03 AC 127 ms
41,576 KB
testcase_04 AC 119 ms
40,188 KB
testcase_05 AC 132 ms
41,768 KB
testcase_06 AC 116 ms
39,828 KB
testcase_07 AC 128 ms
41,468 KB
testcase_08 TLE -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int k = sc.nextInt();
        int s = sc.nextInt();
        int t = sc.nextInt();
        ArrayList<TreeSet<Integer>> floors = new ArrayList<>();
        ArrayList<HashMap<Integer, ArrayList<Integer>>> graph = new ArrayList<>();
        ArrayList<HashMap<Integer, Long>> costs = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            floors.add(new TreeSet<>());
            graph.add(new HashMap<>());
            costs.add(new HashMap<>());
        }
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt();
            int c = sc.nextInt();
            floors.get(a).add(b);
            floors.get(a + 1).add(c);
            if (!graph.get(a).containsKey(b)) {
                graph.get(a).put(b, new ArrayList<>());
            }
            graph.get(a).get(b).add(c);
        }
        floors.get(0).add(s);
        floors.get(n - 1).add(t);
        PriorityQueue<Path> queue = new PriorityQueue<>();
        queue.add(new Path(0, s, 0));
        while (queue.size() > 0 && !costs.get(n - 1).containsKey(t)) {
            Path p = queue.poll();
            if (costs.get(p.idx).containsKey(p.col)) {
                continue;
            }
            costs.get(p.idx).put(p.col, p.value);
            Integer up = floors.get(p.idx).higher(p.col);
            if (up != null) {
                queue.add(new Path(p.idx, up, p.value + up - p.col));
            }
            Integer down = floors.get(p.idx).lower(p.col);
            if (down != null) {
                queue.add(new Path(p.idx, down, p.value + p.col - down));
            }
            if (graph.get(p.idx).containsKey(p.col)) {
                for (int x : graph.get(p.idx).get(p.col)) {
                    queue.add(new Path(p.idx + 1, x, p.value));
                }
            }
        }
        if (costs.get(n - 1).containsKey(t)) {
            System.out.println(costs.get(n - 1).get(t));
        } else {
            System.out.println(-1);
        }
    }
    
    static class Path implements Comparable<Path> {
        int idx;
        int col;
        long value;
        
        public Path(int idx, int col, long value) {
            this.idx = idx;
            this.col = col;
            this.value = value;
        }
        
        public int compareTo(Path another) {
            if (value == another.value) {
                return 0;
            } else if (value < another.value) {
                return -1;
            } else {
                return 1;
            }
        }
    }
}
0