結果
問題 | No.160 最短経路のうち辞書順最小 |
ユーザー | htensai |
提出日時 | 2020-01-15 19:07:31 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 612 ms / 5,000 ms |
コード長 | 2,582 bytes |
コンパイル時間 | 1,958 ms |
コンパイル使用メモリ | 79,932 KB |
実行使用メモリ | 80,780 KB |
最終ジャッジ日時 | 2024-06-11 16:21:58 |
合計ジャッジ時間 | 10,128 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 103 ms
53,128 KB |
testcase_01 | AC | 103 ms
53,340 KB |
testcase_02 | AC | 99 ms
52,996 KB |
testcase_03 | AC | 116 ms
54,000 KB |
testcase_04 | AC | 250 ms
58,212 KB |
testcase_05 | AC | 340 ms
61,432 KB |
testcase_06 | AC | 450 ms
61,360 KB |
testcase_07 | AC | 220 ms
58,288 KB |
testcase_08 | AC | 225 ms
58,200 KB |
testcase_09 | AC | 205 ms
57,828 KB |
testcase_10 | AC | 226 ms
58,828 KB |
testcase_11 | AC | 231 ms
58,520 KB |
testcase_12 | AC | 212 ms
58,056 KB |
testcase_13 | AC | 212 ms
58,428 KB |
testcase_14 | AC | 221 ms
58,280 KB |
testcase_15 | AC | 214 ms
58,176 KB |
testcase_16 | AC | 213 ms
57,900 KB |
testcase_17 | AC | 213 ms
58,648 KB |
testcase_18 | AC | 210 ms
58,188 KB |
testcase_19 | AC | 211 ms
58,220 KB |
testcase_20 | AC | 213 ms
57,768 KB |
testcase_21 | AC | 208 ms
58,072 KB |
testcase_22 | AC | 209 ms
57,924 KB |
testcase_23 | AC | 234 ms
58,496 KB |
testcase_24 | AC | 230 ms
58,616 KB |
testcase_25 | AC | 217 ms
57,744 KB |
testcase_26 | AC | 213 ms
58,192 KB |
testcase_27 | AC | 183 ms
57,068 KB |
testcase_28 | AC | 612 ms
80,780 KB |
testcase_29 | AC | 165 ms
54,540 KB |
ソースコード
import java.util.*; import java.math.*; public class Main { static int[] base; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int s = sc.nextInt(); int g = sc.nextInt(); ArrayList<Path>[] graph = new ArrayList[n]; for (int i = 0; i < n; i++) { graph[i] = new ArrayList<Path>(); } for (int i = 0; i < m; i++) { int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); graph[a].add(new Path(b, c)); graph[b].add(new Path(a, c)); } PriorityQueue<Route> queue = new PriorityQueue<>(); ArrayList<Integer> list = new ArrayList<>(); list.add(s); queue.add(new Route(s, 0, list)); Route[] routes = new Route[n]; while (queue.size() > 0) { Route r = queue.poll(); if (routes[r.to] != null) { continue; } routes[r.to] = r; for (Path p : graph[r.to]) { ArrayList<Integer> newList = new ArrayList<>(r.name); newList.add(p.to); queue.add(new Route(p.to, r.score + p.score, newList)); } } StringBuilder sbr = new StringBuilder(); for (int i = 0; i < routes[g].name.size(); i++) { if (i != 0) { sbr.append(" "); } sbr.append(routes[g].name.get(i)); } System.out.println(sbr); } static class Route implements Comparable<Route> { int to; int score; ArrayList<Integer> name; public Route (int to, int score, ArrayList<Integer> name) { this.to = to; this.score = score; this.name = name; } public int compareTo(Route another) { if (score == another.score) { for (int i = 0; i < name.size() && i < another.name.size(); i++) { if (name.get(i) != another.name.get(i)) { return name.get(i) - another.name.get(i); } } return name.size() - another.name.size(); } else { return score - another.score; } } } static class Path { int to; int score; public Path(int to, int score) { this.to = to; this.score = score; } } }