結果

問題 No.748 yuki国のお財布事情
ユーザー tentententen
提出日時 2020-09-01 18:04:52
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,134 ms / 2,000 ms
コード長 2,111 bytes
コンパイル時間 3,218 ms
コンパイル使用メモリ 73,556 KB
実行使用メモリ 67,208 KB
最終ジャッジ日時 2023-08-12 15:48:45
合計ジャッジ時間 18,856 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 108 ms
55,744 KB
testcase_01 AC 107 ms
55,748 KB
testcase_02 AC 130 ms
56,012 KB
testcase_03 AC 117 ms
55,844 KB
testcase_04 AC 111 ms
55,720 KB
testcase_05 AC 115 ms
56,456 KB
testcase_06 AC 118 ms
55,504 KB
testcase_07 AC 125 ms
56,068 KB
testcase_08 AC 119 ms
56,176 KB
testcase_09 AC 119 ms
55,756 KB
testcase_10 AC 116 ms
55,936 KB
testcase_11 AC 141 ms
56,064 KB
testcase_12 AC 129 ms
55,640 KB
testcase_13 AC 380 ms
60,820 KB
testcase_14 AC 489 ms
60,708 KB
testcase_15 AC 441 ms
58,860 KB
testcase_16 AC 663 ms
65,328 KB
testcase_17 AC 905 ms
66,352 KB
testcase_18 AC 1,042 ms
66,460 KB
testcase_19 AC 1,134 ms
66,944 KB
testcase_20 AC 994 ms
64,756 KB
testcase_21 AC 1,055 ms
67,208 KB
testcase_22 AC 110 ms
55,756 KB
testcase_23 AC 110 ms
56,208 KB
testcase_24 AC 109 ms
56,096 KB
testcase_25 AC 816 ms
66,856 KB
testcase_26 AC 904 ms
66,944 KB
testcase_27 AC 926 ms
66,360 KB
testcase_28 AC 808 ms
66,692 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
    	Scanner sc = new Scanner(System.in);
    	int n = sc.nextInt();
    	int m = sc.nextInt();
    	int k = sc.nextInt();
    	Road[] roads = new Road[m];
    	long total = 0;
    	for (int i = 0; i < m; i++) {
    	    roads[i] = new Road(sc.nextInt() - 1, sc.nextInt() - 1, sc.nextInt());
    	    total += roads[i].cost;
    	}
    	UnionFindTree uft = new UnionFindTree(n);
    	long cost = 0;
    	for (int i = 0; i < k; i++) {
    	    int x = sc.nextInt() - 1;
    	    cost += roads[x].cost;
    	    uft.unite(roads[x]);
    	}
    	Arrays.sort(roads);
    	for (Road r : roads) {
    	    if (!uft.same(r)) {
    	        cost += r.cost;
    	        uft.unite(r);
    	    }
    	}
    	System.out.println(total - cost);
    }
    
    static class UnionFindTree {
        int[] parents;
        
        public UnionFindTree(int size) {
            parents = new int[size];
            for (int i = 0; i < size; i++) {
                parents[i] = i;
            }
        }
        
        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(Road r) {
            return same(r.left, r.right);
        }
        
        public void unite(int x, int y) {
            if (!same(x, y)) {
                parents[(find(x))] = find(y);
            }
        }
        
        public void unite(Road r) {
            unite(r.left, r.right);
        }
    }
    
    static class Road implements Comparable<Road> {
        int left;
        int right;
        int cost;
        
        public Road(int left, int right, int cost) {
            this.left = left;
            this.right = right;
            this.cost = cost;
        }
        
        public int compareTo(Road another) {
            return cost - another.cost;
        }
    }
}
0