結果

問題 No.2200 Weird Shortest Path
ユーザー tentententen
提出日時 2023-03-30 10:55:09
言語 Java21
(openjdk 21)
結果
AC  
実行時間 902 ms / 2,000 ms
コード長 3,129 bytes
コンパイル時間 2,609 ms
コンパイル使用メモリ 89,768 KB
実行使用メモリ 68,648 KB
最終ジャッジ日時 2024-09-21 22:01:18
合計ジャッジ時間 26,498 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
50,420 KB
testcase_01 AC 56 ms
49,984 KB
testcase_02 AC 56 ms
50,496 KB
testcase_03 AC 82 ms
51,468 KB
testcase_04 AC 127 ms
52,468 KB
testcase_05 AC 54 ms
50,452 KB
testcase_06 AC 60 ms
50,652 KB
testcase_07 AC 87 ms
51,532 KB
testcase_08 AC 106 ms
51,836 KB
testcase_09 AC 103 ms
51,672 KB
testcase_10 AC 57 ms
50,440 KB
testcase_11 AC 55 ms
50,444 KB
testcase_12 AC 59 ms
50,408 KB
testcase_13 AC 55 ms
50,364 KB
testcase_14 AC 57 ms
50,432 KB
testcase_15 AC 91 ms
51,416 KB
testcase_16 AC 55 ms
50,468 KB
testcase_17 AC 371 ms
58,992 KB
testcase_18 AC 544 ms
61,528 KB
testcase_19 AC 795 ms
63,508 KB
testcase_20 AC 730 ms
63,616 KB
testcase_21 AC 387 ms
59,092 KB
testcase_22 AC 486 ms
61,344 KB
testcase_23 AC 902 ms
63,424 KB
testcase_24 AC 279 ms
58,024 KB
testcase_25 AC 683 ms
63,512 KB
testcase_26 AC 753 ms
63,384 KB
testcase_27 AC 392 ms
59,124 KB
testcase_28 AC 621 ms
63,812 KB
testcase_29 AC 778 ms
63,364 KB
testcase_30 AC 743 ms
63,204 KB
testcase_31 AC 549 ms
61,408 KB
testcase_32 AC 817 ms
63,608 KB
testcase_33 AC 799 ms
63,428 KB
testcase_34 AC 868 ms
63,480 KB
testcase_35 AC 757 ms
63,660 KB
testcase_36 AC 818 ms
63,916 KB
testcase_37 AC 799 ms
63,300 KB
testcase_38 AC 741 ms
63,596 KB
testcase_39 AC 466 ms
61,528 KB
testcase_40 AC 497 ms
68,648 KB
testcase_41 AC 452 ms
61,288 KB
testcase_42 AC 492 ms
67,676 KB
testcase_43 AC 457 ms
62,884 KB
testcase_44 AC 456 ms
63,092 KB
testcase_45 AC 470 ms
62,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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());
        }
        Arrays.sort(bridges);
        UnionFindTree uft = new UnionFindTree(n);
        long ans = 0;
        for (Bridge x : bridges) {
            ans += uft.unite(x);
        }
        System.out.println(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;
        }
    }
    
    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 (parents[x] != x) {
                parents[x] = find(parents[x]);
            }
            return parents[x];
        }
        
        public long unite(Bridge b) {
            int x = find(b.left);
            int y = find(b.right);
            if (x == y) {
                return 0;
            }
            long ans = (long)counts[x] * counts[y] * b.value;
            parents[x] = y;
            counts[y] += counts[x];
            return ans;
        }
    }
}
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();
    }
    
}
0