結果
問題 | No.160 最短経路のうち辞書順最小 |
ユーザー | htensai |
提出日時 | 2020-01-15 19:07:31 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 794 ms / 5,000 ms |
コード長 | 2,582 bytes |
コンパイル時間 | 3,376 ms |
コンパイル使用メモリ | 76,872 KB |
実行使用メモリ | 83,448 KB |
最終ジャッジ日時 | 2023-09-02 09:35:39 |
合計ジャッジ時間 | 11,840 ms |
ジャッジサーバーID (参考情報) |
judge16 / judge12 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 124 ms
55,584 KB |
testcase_01 | AC | 128 ms
55,592 KB |
testcase_02 | AC | 130 ms
55,984 KB |
testcase_03 | AC | 136 ms
55,876 KB |
testcase_04 | AC | 293 ms
60,032 KB |
testcase_05 | AC | 365 ms
62,444 KB |
testcase_06 | AC | 475 ms
63,156 KB |
testcase_07 | AC | 240 ms
59,672 KB |
testcase_08 | AC | 252 ms
59,868 KB |
testcase_09 | AC | 234 ms
59,760 KB |
testcase_10 | AC | 261 ms
60,172 KB |
testcase_11 | AC | 262 ms
60,076 KB |
testcase_12 | AC | 250 ms
59,944 KB |
testcase_13 | AC | 241 ms
59,628 KB |
testcase_14 | AC | 249 ms
58,952 KB |
testcase_15 | AC | 245 ms
60,084 KB |
testcase_16 | AC | 243 ms
59,636 KB |
testcase_17 | AC | 248 ms
59,668 KB |
testcase_18 | AC | 245 ms
60,180 KB |
testcase_19 | AC | 245 ms
59,660 KB |
testcase_20 | AC | 246 ms
60,008 KB |
testcase_21 | AC | 242 ms
59,364 KB |
testcase_22 | AC | 245 ms
59,824 KB |
testcase_23 | AC | 257 ms
59,968 KB |
testcase_24 | AC | 251 ms
60,888 KB |
testcase_25 | AC | 247 ms
59,684 KB |
testcase_26 | AC | 239 ms
57,228 KB |
testcase_27 | AC | 202 ms
58,564 KB |
testcase_28 | AC | 794 ms
83,448 KB |
testcase_29 | AC | 194 ms
57,128 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; } } }