結果

問題 No.614 壊れたキャンパス
ユーザー tentententen
提出日時 2021-03-10 08:33:48
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,888 bytes
コンパイル時間 2,183 ms
コンパイル使用メモリ 80,304 KB
実行使用メモリ 189,576 KB
最終ジャッジ日時 2024-04-20 05:05:14
合計ジャッジ時間 25,126 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
37,328 KB
testcase_01 AC 50 ms
37,152 KB
testcase_02 AC 51 ms
37,136 KB
testcase_03 AC 50 ms
36,724 KB
testcase_04 AC 50 ms
37,328 KB
testcase_05 AC 50 ms
36,728 KB
testcase_06 AC 51 ms
36,984 KB
testcase_07 AC 51 ms
37,108 KB
testcase_08 TLE -
testcase_09 TLE -
testcase_10 AC 1,359 ms
149,536 KB
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 AC 1,411 ms
131,856 KB
testcase_16 TLE -
testcase_17 AC 1,428 ms
189,576 KB
testcase_18 AC 1,788 ms
146,996 KB
testcase_19 AC 837 ms
110,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] first = br.readLine().split(" ", 5);
        int n = Integer.parseInt(first[0]);
        int m = Integer.parseInt(first[1]);
        int k = Integer.parseInt(first[2]);
        int s = Integer.parseInt(first[3]);
        int t = Integer.parseInt(first[4]);
        ArrayList<TreeSet<Integer>> floors = new ArrayList<>();
        ArrayList<HashMap<Integer, ArrayList<Integer>>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            floors.add(new TreeSet<>());
            graph.add(new HashMap<>());
        }
        for (int i = 0; i < m; i++) {
            String[] line = br.readLine().split(" ", 3);
            int a = Integer.parseInt(line[0]) - 1;
            int b = Integer.parseInt(line[1]);
            int c = Integer.parseInt(line[2]);
            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) {
            Path p = queue.poll();
            if (!floors.get(p.idx).contains(p.col)) {
                continue;
            }
            if (p.idx == n - 1 && p.col == t) {
                System.out.println(p.value);
                return;
            }
            floors.get(p.idx).remove(p.col);
            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));
                }
            }
        }
        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