結果

問題 No.2200 Weird Shortest Path
ユーザー tentententen
提出日時 2023-02-10 13:43:54
言語 Java21
(openjdk 21)
結果
AC  
実行時間 748 ms / 2,000 ms
コード長 3,311 bytes
コンパイル時間 4,565 ms
コンパイル使用メモリ 79,832 KB
実行使用メモリ 54,172 KB
最終ジャッジ日時 2023-09-21 16:32:35
合計ジャッジ時間 32,067 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
33,968 KB
testcase_01 AC 42 ms
33,700 KB
testcase_02 AC 43 ms
33,744 KB
testcase_03 AC 71 ms
34,840 KB
testcase_04 AC 113 ms
38,040 KB
testcase_05 AC 42 ms
33,680 KB
testcase_06 AC 49 ms
33,968 KB
testcase_07 AC 59 ms
35,096 KB
testcase_08 AC 94 ms
35,980 KB
testcase_09 AC 74 ms
35,512 KB
testcase_10 AC 44 ms
33,908 KB
testcase_11 AC 43 ms
33,808 KB
testcase_12 AC 46 ms
33,912 KB
testcase_13 AC 42 ms
33,776 KB
testcase_14 AC 44 ms
33,712 KB
testcase_15 AC 85 ms
35,284 KB
testcase_16 AC 44 ms
33,740 KB
testcase_17 AC 349 ms
45,700 KB
testcase_18 AC 471 ms
47,844 KB
testcase_19 AC 717 ms
49,684 KB
testcase_20 AC 647 ms
49,988 KB
testcase_21 AC 363 ms
46,004 KB
testcase_22 AC 439 ms
46,720 KB
testcase_23 AC 725 ms
49,740 KB
testcase_24 AC 252 ms
44,692 KB
testcase_25 AC 646 ms
49,448 KB
testcase_26 AC 687 ms
49,872 KB
testcase_27 AC 360 ms
46,044 KB
testcase_28 AC 529 ms
47,740 KB
testcase_29 AC 748 ms
49,656 KB
testcase_30 AC 671 ms
49,784 KB
testcase_31 AC 493 ms
47,476 KB
testcase_32 AC 709 ms
49,656 KB
testcase_33 AC 698 ms
50,072 KB
testcase_34 AC 720 ms
49,916 KB
testcase_35 AC 717 ms
49,952 KB
testcase_36 AC 715 ms
49,680 KB
testcase_37 AC 689 ms
49,976 KB
testcase_38 AC 744 ms
50,280 KB
testcase_39 AC 458 ms
47,180 KB
testcase_40 AC 506 ms
54,172 KB
testcase_41 AC 403 ms
47,168 KB
testcase_42 AC 426 ms
52,308 KB
testcase_43 AC 427 ms
49,328 KB
testcase_44 AC 434 ms
48,552 KB
testcase_45 AC 425 ms
49,352 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();
        Path[] paths = new Path[m];
        for (int i = 0; i < m; i++) {
            paths[i] = new Path(sc.nextInt() - 1, sc.nextInt() - 1, sc.nextInt());
        }
        Arrays.sort(paths);
        UnionFindTree uft = new UnionFindTree(n);
        long ans = 0;
        for (Path x : paths) {
            if (!uft.same(x)) {
                ans += uft.unite(x);
            }
        }
        System.out.println(ans);
    }
    
    static class Path implements Comparable<Path> {
        int left;
        int right;
        int value;
        
        public Path(int left, int right, int value) {
            this.left = left;
            this.right = right;
            this.value = value;
        }
        
        public int compareTo(Path 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 (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(Path x) {
            return same(x.left, x.right);
        }
        
        public long unite(Path p) {
            int x = find(p.left);
            int y = find(p.right);
            long ans = (long)p.value * counts[x] * counts[y];
            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