結果

問題 No.748 yuki国のお財布事情
ユーザー htensaihtensai
提出日時 2020-01-30 18:18:51
言語 Java19
(openjdk 21)
結果
AC  
実行時間 438 ms / 2,000 ms
コード長 2,961 bytes
コンパイル時間 2,503 ms
コンパイル使用メモリ 74,552 KB
実行使用メモリ 61,064 KB
最終ジャッジ日時 2023-10-14 08:38:35
合計ジャッジ時間 9,132 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
49,604 KB
testcase_01 AC 37 ms
49,720 KB
testcase_02 AC 39 ms
49,620 KB
testcase_03 AC 37 ms
49,708 KB
testcase_04 AC 38 ms
49,480 KB
testcase_05 AC 40 ms
50,148 KB
testcase_06 AC 38 ms
49,500 KB
testcase_07 AC 38 ms
49,772 KB
testcase_08 AC 38 ms
49,492 KB
testcase_09 AC 39 ms
49,804 KB
testcase_10 AC 37 ms
49,976 KB
testcase_11 AC 40 ms
49,596 KB
testcase_12 AC 38 ms
50,120 KB
testcase_13 AC 161 ms
56,348 KB
testcase_14 AC 189 ms
56,660 KB
testcase_15 AC 167 ms
56,552 KB
testcase_16 AC 228 ms
57,932 KB
testcase_17 AC 392 ms
60,760 KB
testcase_18 AC 403 ms
60,832 KB
testcase_19 AC 393 ms
60,576 KB
testcase_20 AC 367 ms
59,968 KB
testcase_21 AC 391 ms
60,408 KB
testcase_22 AC 38 ms
49,596 KB
testcase_23 AC 38 ms
49,792 KB
testcase_24 AC 37 ms
49,708 KB
testcase_25 AC 395 ms
60,500 KB
testcase_26 AC 426 ms
61,064 KB
testcase_27 AC 438 ms
61,044 KB
testcase_28 AC 257 ms
59,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
    static int[] dp;
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] first = br.readLine().split(" ", 3);
        int n = Integer.parseInt(first[0]);
        int m = Integer.parseInt(first[1]);
        int k = Integer.parseInt(first[2]);
        Route[] routes = new Route[m];
        long total = 0;
        for (int i = 0; i < m; i++) {
            routes[i] = new Route(br.readLine());
            total += routes[i].cost;
        }
        UnionFindTree uft = new UnionFindTree(n);
        for (int i = 0; i < k; i++) {
            int idx = Integer.parseInt(br.readLine()) - 1;
            uft.unite(routes[idx]);
            total -= routes[idx].cost;
            routes[idx].cost = Integer.MAX_VALUE;
        }
        Arrays.sort(routes);
        for (int i = 0; i < m && !uft.isFull(); i++) {
            if (!uft.same(routes[i])) {
                total -= routes[i].cost;
                uft.unite(routes[i]);
            }
        }
        System.out.println(total);
    }
    
    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) {
                return x;
            } else {
                return parents[x] = find(parents[x]);
            }
        }
        
        public boolean same(int x, int y) {
            return find(x) == find(y);
        }
        
        public boolean same(Route r) {
            return same(r.left, r.right);
        }
        
        public void unite(int x, int y) {
            int xx = find(x);
            int yy = find(y);
            if (xx == yy) {
                return;
            } else if (xx > yy) {
                parents[xx] = yy;
                counts[yy] += parents[xx];
            } else {
                parents[yy] = xx;
                counts[xx] += parents[yy];
            }
        }
        
        public void unite(Route r) {
            unite(r.left, r.right);
        }
        
        public boolean isFull() {
            return counts[0] == counts.length;
        }
    }
    
    static class Route implements Comparable<Route> {
        int left;
        int right;
        int cost;
        
        public Route(String s) {
            String[] arr = s.split(" ", 3);
            left = Integer.parseInt(arr[0]) - 1;
            right = Integer.parseInt(arr[1]) - 1;
            cost = Integer.parseInt(arr[2]);
        }
        
        public int compareTo(Route another) {
            return cost - another.cost;
        }
    }
}
0