結果
問題 | No.2200 Weird Shortest Path |
ユーザー | tenten |
提出日時 | 2023-07-26 08:59:03 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 875 ms / 2,000 ms |
コード長 | 3,340 bytes |
コンパイル時間 | 3,381 ms |
コンパイル使用メモリ | 91,728 KB |
実行使用メモリ | 57,800 KB |
最終ジャッジ日時 | 2024-10-02 22:40:37 |
合計ジャッジ時間 | 26,949 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 56 ms
37,324 KB |
testcase_01 | AC | 58 ms
37,420 KB |
testcase_02 | AC | 56 ms
37,156 KB |
testcase_03 | AC | 88 ms
38,276 KB |
testcase_04 | AC | 128 ms
39,940 KB |
testcase_05 | AC | 56 ms
37,352 KB |
testcase_06 | AC | 63 ms
37,360 KB |
testcase_07 | AC | 77 ms
38,168 KB |
testcase_08 | AC | 109 ms
39,296 KB |
testcase_09 | AC | 100 ms
38,400 KB |
testcase_10 | AC | 57 ms
37,276 KB |
testcase_11 | AC | 56 ms
37,268 KB |
testcase_12 | AC | 58 ms
37,224 KB |
testcase_13 | AC | 56 ms
37,320 KB |
testcase_14 | AC | 56 ms
36,860 KB |
testcase_15 | AC | 94 ms
38,804 KB |
testcase_16 | AC | 58 ms
37,248 KB |
testcase_17 | AC | 381 ms
49,124 KB |
testcase_18 | AC | 550 ms
50,720 KB |
testcase_19 | AC | 748 ms
52,664 KB |
testcase_20 | AC | 752 ms
53,628 KB |
testcase_21 | AC | 396 ms
49,036 KB |
testcase_22 | AC | 490 ms
50,356 KB |
testcase_23 | AC | 782 ms
54,592 KB |
testcase_24 | AC | 283 ms
48,516 KB |
testcase_25 | AC | 785 ms
52,816 KB |
testcase_26 | AC | 842 ms
53,972 KB |
testcase_27 | AC | 401 ms
48,912 KB |
testcase_28 | AC | 618 ms
51,780 KB |
testcase_29 | AC | 804 ms
54,076 KB |
testcase_30 | AC | 779 ms
53,944 KB |
testcase_31 | AC | 560 ms
51,256 KB |
testcase_32 | AC | 725 ms
54,300 KB |
testcase_33 | AC | 875 ms
53,804 KB |
testcase_34 | AC | 829 ms
55,036 KB |
testcase_35 | AC | 757 ms
54,380 KB |
testcase_36 | AC | 756 ms
54,056 KB |
testcase_37 | AC | 808 ms
54,320 KB |
testcase_38 | AC | 761 ms
53,824 KB |
testcase_39 | AC | 485 ms
49,540 KB |
testcase_40 | AC | 499 ms
57,800 KB |
testcase_41 | AC | 482 ms
50,604 KB |
testcase_42 | AC | 503 ms
56,568 KB |
testcase_43 | AC | 478 ms
52,648 KB |
testcase_44 | AC | 463 ms
52,836 KB |
testcase_45 | AC | 472 ms
52,304 KB |
ソースコード
import java.io.*; import java.util.*; import java.util.stream.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int m = sc.nextInt(); Bridge[] bridges = new Bridge[m]; for (int i = 0; i < m; i++) { bridges[i] = new Bridge(sc.nextInt() - 1, sc.nextInt() - 1, sc.nextInt()); } UnionFindTree uft = new UnionFindTree(n); Arrays.sort(bridges); long ans = 0; for (Bridge x : bridges) { if (!uft.same(x)) { ans += uft.unite(x); } } System.out.println(ans); } static class UnionFindTree { int[] parents; int[] counts; public UnionFindTree(int size) { parents = new int[size]; counts = new int[size]; for (int i = 0; i < size; i++) { parents[i] = i; counts[i] = 1; } } public int find(int x) { if (x == parents[x]) { return x; } else { return parents[x] = find(parents[x]); } } public boolean same(int x, int y) { return find(x) == find(y); } public boolean same(Bridge b) { return same(b.left, b.right); } public long unite(Bridge b) { int x = find(b.left); int y = find(b.right); long ans = (long)counts[x] * counts[y] * b.value; parents[x] = y; counts[y] += counts[x]; return ans; } } static class Bridge implements Comparable<Bridge> { int left; int right; int value; public Bridge(int left, int right, int value) { this.left = left; this.right = right; this.value = value; } public int compareTo(Bridge another) { return value - another.value; } } } class Utilities { static String arrayToLineString(Object[] arr) { return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n")); } static String arrayToLineString(int[] arr) { return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new)); } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public int[] nextIntArray() throws Exception { return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }