結果

問題 No.748 yuki国のお財布事情
ユーザー 37zigen37zigen
提出日時 2020-04-01 16:14:44
言語 Java21
(openjdk 21)
結果
AC  
実行時間 938 ms / 2,000 ms
コード長 1,777 bytes
コンパイル時間 3,637 ms
コンパイル使用メモリ 81,124 KB
実行使用メモリ 73,408 KB
最終ジャッジ日時 2023-09-09 05:38:26
合計ジャッジ時間 15,479 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
55,956 KB
testcase_01 AC 127 ms
55,908 KB
testcase_02 AC 128 ms
55,876 KB
testcase_03 AC 126 ms
55,672 KB
testcase_04 AC 128 ms
55,696 KB
testcase_05 AC 126 ms
55,880 KB
testcase_06 AC 130 ms
55,960 KB
testcase_07 AC 131 ms
55,592 KB
testcase_08 AC 133 ms
55,912 KB
testcase_09 AC 132 ms
53,316 KB
testcase_10 AC 133 ms
55,652 KB
testcase_11 AC 135 ms
55,684 KB
testcase_12 AC 133 ms
55,532 KB
testcase_13 AC 358 ms
60,672 KB
testcase_14 AC 413 ms
60,412 KB
testcase_15 AC 376 ms
61,016 KB
testcase_16 AC 536 ms
63,892 KB
testcase_17 AC 793 ms
67,020 KB
testcase_18 AC 938 ms
73,408 KB
testcase_19 AC 776 ms
65,336 KB
testcase_20 AC 826 ms
65,200 KB
testcase_21 AC 788 ms
65,212 KB
testcase_22 AC 127 ms
55,892 KB
testcase_23 AC 125 ms
55,680 KB
testcase_24 AC 125 ms
55,916 KB
testcase_25 AC 805 ms
69,544 KB
testcase_26 AC 930 ms
67,724 KB
testcase_27 AC 917 ms
67,616 KB
testcase_28 AC 661 ms
66,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Scanner;

class DJSet{
	int n;
	int[] upper;
	
	public DJSet(int n) {
		this.n=n;
		upper=new int[n];
		Arrays.fill(upper, -1);
	}

	int root(int x) {
		return upper[x]<0?x:(upper[x]=root(upper[x]));
	}
	
	boolean equiv(int x,int y) {
		return root(x)==root(y);
	}
	
	void setUnion(int x,int y) {
		x=root(x);y=root(y);
		if(x==y)return;
		if(upper[x]<upper[y]) {
			x^=y;y^=x;x^=y;
		}
		upper[y]+=upper[x];
		//if(upper[x]==upper[y])--upper[y];
		upper[x]=y;
	}
}

class Edge implements Comparable<Edge>{
	int src,dst,cost;
	
	public Edge(int src,int dst,int cost) {
		this.src=src;
		this.dst=dst;
		this.cost=cost;
	}
	@Override
	public int compareTo(Edge o) {
		return cost-o.cost;
	}
}

class Main {
	public static void main(String[] args) {
		new Main().run();
	}
	
	void run() {
		Scanner sc=new Scanner(System.in);
		int N=sc.nextInt();
		int M=sc.nextInt();
		int K=sc.nextInt();
		DJSet ds=new DJSet(N);
		PriorityQueue<Edge> pq=new PriorityQueue<>();
		int[] a=new int[M];
		int[] b=new int[M];
		int[] c=new int[M];
		boolean[] fixed=new boolean[M];
		for(int i=0;i<M;++i) {
			a[i]=Integer.parseInt(sc.next());
			b[i]=Integer.parseInt(sc.next());
			c[i]=Integer.parseInt(sc.next());
			--a[i];--b[i];
		}
		long ans=0;
		for(int i=0;i<K;++i) {
			int e=Integer.parseInt(sc.next());
			--e;
			ds.setUnion(a[e], b[e]);
			fixed[e]=true;
		}
		for(int i=0;i<M;++i) if(!fixed[i]) pq.add(new Edge(a[i], b[i], c[i]));
		while(!pq.isEmpty()) {
			Edge e=pq.poll();
			if(ds.equiv(e.src, e.dst)) {
				ans+=e.cost;
			}else {
				ds.setUnion(e.src, e.dst);
			}
		}
		System.out.println(ans);
	}
	
	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}

0