結果

問題 No.2200 Weird Shortest Path
ユーザー tentententen
提出日時 2023-03-30 10:55:09
言語 Java21
(openjdk 21)
結果
AC  
実行時間 860 ms / 2,000 ms
コード長 3,129 bytes
コンパイル時間 4,632 ms
コンパイル使用メモリ 84,196 KB
実行使用メモリ 71,560 KB
最終ジャッジ日時 2023-10-21 20:35:36
合計ジャッジ時間 33,475 ms
ジャッジサーバーID
(参考情報)
judge11 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
53,620 KB
testcase_01 AC 58 ms
53,616 KB
testcase_02 AC 58 ms
53,608 KB
testcase_03 AC 82 ms
54,516 KB
testcase_04 AC 121 ms
55,716 KB
testcase_05 AC 57 ms
53,620 KB
testcase_06 AC 65 ms
54,196 KB
testcase_07 AC 87 ms
52,516 KB
testcase_08 AC 111 ms
55,456 KB
testcase_09 AC 105 ms
54,484 KB
testcase_10 AC 61 ms
53,620 KB
testcase_11 AC 57 ms
53,620 KB
testcase_12 AC 59 ms
53,624 KB
testcase_13 AC 57 ms
52,568 KB
testcase_14 AC 59 ms
52,504 KB
testcase_15 AC 96 ms
54,480 KB
testcase_16 AC 60 ms
52,576 KB
testcase_17 AC 384 ms
62,248 KB
testcase_18 AC 531 ms
64,564 KB
testcase_19 AC 754 ms
66,636 KB
testcase_20 AC 746 ms
66,336 KB
testcase_21 AC 405 ms
62,544 KB
testcase_22 AC 506 ms
64,504 KB
testcase_23 AC 837 ms
66,616 KB
testcase_24 AC 291 ms
61,616 KB
testcase_25 AC 757 ms
66,320 KB
testcase_26 AC 831 ms
66,744 KB
testcase_27 AC 398 ms
62,472 KB
testcase_28 AC 601 ms
66,776 KB
testcase_29 AC 817 ms
66,364 KB
testcase_30 AC 832 ms
66,564 KB
testcase_31 AC 551 ms
64,716 KB
testcase_32 AC 807 ms
66,396 KB
testcase_33 AC 790 ms
66,368 KB
testcase_34 AC 860 ms
66,368 KB
testcase_35 AC 806 ms
66,616 KB
testcase_36 AC 724 ms
66,688 KB
testcase_37 AC 836 ms
66,576 KB
testcase_38 AC 822 ms
66,116 KB
testcase_39 AC 467 ms
64,492 KB
testcase_40 AC 488 ms
71,540 KB
testcase_41 AC 441 ms
64,252 KB
testcase_42 AC 472 ms
71,560 KB
testcase_43 AC 460 ms
66,080 KB
testcase_44 AC 459 ms
66,124 KB
testcase_45 AC 454 ms
66,144 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