結果

問題 No.748 yuki国のお財布事情
ユーザー 37zigen37zigen
提出日時 2020-04-01 16:13:25
言語 Java19
(openjdk 21)
結果
AC  
実行時間 990 ms / 2,000 ms
コード長 1,753 bytes
コンパイル時間 2,375 ms
コンパイル使用メモリ 81,340 KB
実行使用メモリ 72,604 KB
最終ジャッジ日時 2023-09-09 05:37:10
合計ジャッジ時間 15,925 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
55,780 KB
testcase_01 AC 126 ms
55,544 KB
testcase_02 AC 128 ms
55,760 KB
testcase_03 AC 123 ms
55,536 KB
testcase_04 AC 124 ms
55,908 KB
testcase_05 AC 124 ms
56,060 KB
testcase_06 AC 127 ms
55,644 KB
testcase_07 AC 131 ms
56,036 KB
testcase_08 AC 131 ms
55,644 KB
testcase_09 AC 133 ms
55,968 KB
testcase_10 AC 132 ms
56,004 KB
testcase_11 AC 131 ms
55,656 KB
testcase_12 AC 131 ms
55,912 KB
testcase_13 AC 352 ms
61,016 KB
testcase_14 AC 508 ms
62,300 KB
testcase_15 AC 382 ms
60,924 KB
testcase_16 AC 518 ms
63,808 KB
testcase_17 AC 791 ms
71,076 KB
testcase_18 AC 953 ms
72,544 KB
testcase_19 AC 912 ms
67,656 KB
testcase_20 AC 858 ms
67,564 KB
testcase_21 AC 885 ms
65,272 KB
testcase_22 AC 125 ms
56,096 KB
testcase_23 AC 124 ms
56,264 KB
testcase_24 AC 123 ms
55,724 KB
testcase_25 AC 810 ms
69,768 KB
testcase_26 AC 813 ms
67,380 KB
testcase_27 AC 990 ms
72,604 KB
testcase_28 AC 655 ms
66,276 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;
		}
		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