結果
問題 | No.807 umg tours |
ユーザー | tenten |
提出日時 | 2024-03-07 15:38:14 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,514 ms / 4,000 ms |
コード長 | 2,844 bytes |
コンパイル時間 | 3,669 ms |
コンパイル使用メモリ | 96,928 KB |
実行使用メモリ | 117,172 KB |
最終ジャッジ日時 | 2024-09-29 18:40:25 |
合計ジャッジ時間 | 20,311 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 85 ms
51,276 KB |
testcase_01 | AC | 88 ms
51,348 KB |
testcase_02 | AC | 99 ms
52,256 KB |
testcase_03 | AC | 99 ms
52,200 KB |
testcase_04 | AC | 82 ms
51,368 KB |
testcase_05 | AC | 89 ms
51,416 KB |
testcase_06 | AC | 98 ms
52,144 KB |
testcase_07 | AC | 99 ms
52,288 KB |
testcase_08 | AC | 67 ms
50,944 KB |
testcase_09 | AC | 73 ms
51,036 KB |
testcase_10 | AC | 75 ms
51,340 KB |
testcase_11 | AC | 1,181 ms
106,356 KB |
testcase_12 | AC | 915 ms
90,572 KB |
testcase_13 | AC | 1,219 ms
97,628 KB |
testcase_14 | AC | 665 ms
66,492 KB |
testcase_15 | AC | 586 ms
61,536 KB |
testcase_16 | AC | 1,407 ms
99,660 KB |
testcase_17 | AC | 1,454 ms
110,800 KB |
testcase_18 | AC | 1,514 ms
112,004 KB |
testcase_19 | AC | 1,410 ms
113,540 KB |
testcase_20 | AC | 834 ms
87,912 KB |
testcase_21 | AC | 792 ms
87,976 KB |
testcase_22 | AC | 488 ms
66,244 KB |
testcase_23 | AC | 439 ms
65,188 KB |
testcase_24 | AC | 999 ms
106,364 KB |
testcase_25 | AC | 1,508 ms
117,172 KB |
ソースコード
import java.io.*; import java.util.*; import java.util.function.*; import java.util.stream.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); List<List<Path>> graph = new ArrayList<>(); for (int i = 0; i < n; i++) { graph.add(new ArrayList<>()); } int m = sc.nextInt(); for (int i = 0; i < m; i++) { int a = sc.nextInt() - 1; int b = sc.nextInt() - 1; int c = sc.nextInt(); graph.get(a).add(new Path(0, b, c)); graph.get(b).add(new Path(0, a, c)); } PriorityQueue<Path> queue = new PriorityQueue<>(); queue.add(new Path(0, 0, 0)); long[][] costs = new long[2][n]; for (long[] arr : costs) { Arrays.fill(arr, Long.MAX_VALUE); } costs[1][0] = 0; while (queue.size() > 0) { Path p = queue.poll(); if (costs[p.type][p.idx] <= p.value) { continue; } costs[p.type][p.idx] = p.value; for (Path x : graph.get(p.idx)) { queue.add(new Path(p.type, x.idx, p.value + x.value)); if (p.type == 0) { queue.add(new Path(1, x.idx, p.value)); } } } String result = IntStream.range(0, n) .mapToObj(i -> String.valueOf(costs[0][i] + costs[1][i])) .collect(Collectors.joining("\n")); System.out.println(result); } static class Path implements Comparable<Path> { int type; int idx; long value; public Path(int type, int idx, long value) { this.idx = idx; this.type = type; this.value = value; } public int compareTo(Path another) { return Long.compare(value, another.value); } } } class Scanner { BufferedReader br; StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); } catch (Exception e) { } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { try { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } catch (Exception e) { e.printStackTrace(); } finally { return st.nextToken(); } } }