結果

問題 No.2200 Weird Shortest Path
ユーザー tentententen
提出日時 2023-02-10 13:43:54
言語 Java21
(openjdk 21)
結果
AC  
実行時間 765 ms / 2,000 ms
コード長 3,311 bytes
コンパイル時間 3,690 ms
コンパイル使用メモリ 89,620 KB
実行使用メモリ 57,320 KB
最終ジャッジ日時 2024-07-07 10:15:29
合計ジャッジ時間 22,438 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
37,068 KB
testcase_01 AC 45 ms
37,180 KB
testcase_02 AC 46 ms
37,108 KB
testcase_03 AC 74 ms
38,132 KB
testcase_04 AC 112 ms
39,824 KB
testcase_05 AC 47 ms
36,772 KB
testcase_06 AC 51 ms
36,892 KB
testcase_07 AC 70 ms
37,868 KB
testcase_08 AC 99 ms
39,280 KB
testcase_09 AC 87 ms
38,504 KB
testcase_10 AC 47 ms
37,036 KB
testcase_11 AC 46 ms
37,140 KB
testcase_12 AC 50 ms
37,068 KB
testcase_13 AC 48 ms
37,100 KB
testcase_14 AC 48 ms
37,248 KB
testcase_15 AC 89 ms
38,728 KB
testcase_16 AC 45 ms
37,072 KB
testcase_17 AC 325 ms
48,748 KB
testcase_18 AC 444 ms
50,940 KB
testcase_19 AC 634 ms
52,992 KB
testcase_20 AC 590 ms
52,992 KB
testcase_21 AC 345 ms
49,280 KB
testcase_22 AC 420 ms
50,420 KB
testcase_23 AC 588 ms
53,524 KB
testcase_24 AC 230 ms
46,968 KB
testcase_25 AC 570 ms
52,944 KB
testcase_26 AC 709 ms
53,448 KB
testcase_27 AC 337 ms
49,204 KB
testcase_28 AC 509 ms
51,908 KB
testcase_29 AC 708 ms
53,588 KB
testcase_30 AC 616 ms
53,228 KB
testcase_31 AC 486 ms
51,100 KB
testcase_32 AC 670 ms
53,428 KB
testcase_33 AC 691 ms
53,208 KB
testcase_34 AC 667 ms
53,184 KB
testcase_35 AC 603 ms
53,340 KB
testcase_36 AC 621 ms
53,240 KB
testcase_37 AC 765 ms
53,176 KB
testcase_38 AC 634 ms
53,304 KB
testcase_39 AC 402 ms
50,068 KB
testcase_40 AC 414 ms
57,320 KB
testcase_41 AC 389 ms
50,644 KB
testcase_42 AC 417 ms
56,156 KB
testcase_43 AC 394 ms
52,404 KB
testcase_44 AC 410 ms
52,692 KB
testcase_45 AC 405 ms
52,772 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