結果
問題 |
No.614 壊れたキャンパス
|
ユーザー |
![]() |
提出日時 | 2021-03-09 20:11:06 |
言語 | Java (openjdk 23) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,752 bytes |
コンパイル時間 | 3,528 ms |
コンパイル使用メモリ | 80,548 KB |
実行使用メモリ | 153,784 KB |
最終ジャッジ日時 | 2024-10-11 13:35:14 |
合計ジャッジ時間 | 10,300 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 8 TLE * 1 -- * 11 |
ソースコード
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; } } } }