結果

問題 No.2200 Weird Shortest Path
ユーザー tentententen
提出日時 2023-07-26 08:59:03
言語 Java21
(openjdk 21)
結果
AC  
実行時間 692 ms / 2,000 ms
コード長 3,340 bytes
コンパイル時間 2,935 ms
コンパイル使用メモリ 91,976 KB
実行使用メモリ 68,448 KB
最終ジャッジ日時 2024-04-12 11:00:10
合計ジャッジ時間 21,212 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
50,540 KB
testcase_01 AC 47 ms
50,532 KB
testcase_02 AC 50 ms
50,592 KB
testcase_03 AC 69 ms
51,488 KB
testcase_04 AC 111 ms
52,404 KB
testcase_05 AC 45 ms
50,588 KB
testcase_06 AC 51 ms
50,496 KB
testcase_07 AC 62 ms
51,524 KB
testcase_08 AC 96 ms
52,044 KB
testcase_09 AC 84 ms
51,808 KB
testcase_10 AC 48 ms
50,296 KB
testcase_11 AC 45 ms
50,464 KB
testcase_12 AC 46 ms
50,644 KB
testcase_13 AC 45 ms
50,276 KB
testcase_14 AC 45 ms
50,456 KB
testcase_15 AC 77 ms
51,564 KB
testcase_16 AC 46 ms
50,672 KB
testcase_17 AC 310 ms
58,940 KB
testcase_18 AC 422 ms
61,288 KB
testcase_19 AC 609 ms
63,644 KB
testcase_20 AC 579 ms
63,352 KB
testcase_21 AC 334 ms
59,272 KB
testcase_22 AC 396 ms
61,660 KB
testcase_23 AC 631 ms
65,788 KB
testcase_24 AC 234 ms
58,280 KB
testcase_25 AC 581 ms
63,864 KB
testcase_26 AC 651 ms
65,744 KB
testcase_27 AC 314 ms
59,020 KB
testcase_28 AC 475 ms
63,508 KB
testcase_29 AC 660 ms
65,544 KB
testcase_30 AC 607 ms
63,496 KB
testcase_31 AC 454 ms
61,192 KB
testcase_32 AC 692 ms
65,556 KB
testcase_33 AC 622 ms
65,864 KB
testcase_34 AC 666 ms
65,816 KB
testcase_35 AC 621 ms
65,728 KB
testcase_36 AC 610 ms
65,804 KB
testcase_37 AC 584 ms
65,520 KB
testcase_38 AC 636 ms
66,216 KB
testcase_39 AC 413 ms
61,076 KB
testcase_40 AC 424 ms
68,448 KB
testcase_41 AC 385 ms
61,372 KB
testcase_42 AC 396 ms
68,188 KB
testcase_43 AC 401 ms
62,888 KB
testcase_44 AC 394 ms
63,264 KB
testcase_45 AC 395 ms
63,420 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());
        }
        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();
    }
    
}
0