結果
問題 | No.160 最短経路のうち辞書順最小 |
ユーザー | tenten |
提出日時 | 2020-12-22 15:01:59 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 591 ms / 5,000 ms |
コード長 | 2,312 bytes |
コンパイル時間 | 3,726 ms |
コンパイル使用メモリ | 80,440 KB |
実行使用メモリ | 63,876 KB |
最終ジャッジ日時 | 2023-10-21 12:55:20 |
合計ジャッジ時間 | 13,760 ms |
ジャッジサーバーID (参考情報) |
judge10 / judge15 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 142 ms
57,476 KB |
testcase_01 | AC | 139 ms
57,420 KB |
testcase_02 | AC | 141 ms
57,580 KB |
testcase_03 | AC | 149 ms
55,620 KB |
testcase_04 | AC | 330 ms
61,872 KB |
testcase_05 | AC | 419 ms
62,048 KB |
testcase_06 | AC | 497 ms
62,088 KB |
testcase_07 | AC | 274 ms
61,500 KB |
testcase_08 | AC | 280 ms
61,940 KB |
testcase_09 | AC | 255 ms
61,036 KB |
testcase_10 | AC | 278 ms
61,532 KB |
testcase_11 | AC | 290 ms
61,812 KB |
testcase_12 | AC | 270 ms
61,020 KB |
testcase_13 | AC | 267 ms
61,332 KB |
testcase_14 | AC | 267 ms
61,316 KB |
testcase_15 | AC | 266 ms
61,560 KB |
testcase_16 | AC | 268 ms
61,040 KB |
testcase_17 | AC | 272 ms
61,652 KB |
testcase_18 | AC | 267 ms
61,036 KB |
testcase_19 | AC | 267 ms
61,076 KB |
testcase_20 | AC | 271 ms
61,108 KB |
testcase_21 | AC | 259 ms
61,052 KB |
testcase_22 | AC | 271 ms
61,216 KB |
testcase_23 | AC | 282 ms
61,732 KB |
testcase_24 | AC | 282 ms
61,780 KB |
testcase_25 | AC | 271 ms
61,456 KB |
testcase_26 | AC | 263 ms
61,296 KB |
testcase_27 | AC | 215 ms
57,716 KB |
testcase_28 | AC | 591 ms
63,876 KB |
testcase_29 | AC | 199 ms
57,632 KB |
ソースコード
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 start = sc.nextInt(); int goal = sc.nextInt(); ArrayList<TreeMap<Integer, Integer>> graph = new ArrayList<>(); for (int i = 0; i < n; i++) { graph.add(new TreeMap<>()); } for (int i = 0; i < m; i++) { int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); graph.get(a).put(b, c); graph.get(b).put(a, c); } PriorityQueue<Path> queue = new PriorityQueue<>(); queue.add(new Path(goal, 0)); int[] costs = new int[n]; Arrays.fill(costs, Integer.MAX_VALUE / 2); while (queue.size() > 0) { Path p = queue.poll(); if (costs[p.idx] <= p.value) { continue; } costs[p.idx] = p.value; for (Map.Entry<Integer, Integer> entry : graph.get(p.idx).entrySet()) { queue.add(new Path(entry.getKey(), p.value + entry.getValue())); } } int current = start; int total = 0; ArrayList<Integer> ans = new ArrayList<>(); ans.add(start); while (current != goal) { for (Map.Entry<Integer, Integer> entry : graph.get(current).entrySet()) { if (costs[entry.getKey()] + total + entry.getValue() == costs[start]) { total += entry.getValue(); current = entry.getKey(); ans.add(current); break; } } } StringBuilder sb = new StringBuilder(); for (int i = 0; i < ans.size(); i++) { if (i > 0) { sb.append(" "); } sb.append(ans.get(i)); } System.out.println(sb); } static class Path implements Comparable<Path> { int idx; int value; public Path(int idx, int value) { this.idx = idx; this.value = value; } public int compareTo(Path another) { return value - another.value; } } }