結果
問題 | No.748 yuki国のお財布事情 |
ユーザー | 37zigen |
提出日時 | 2020-04-01 16:13:58 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 926 ms / 2,000 ms |
コード長 | 1,755 bytes |
コンパイル時間 | 2,218 ms |
コンパイル使用メモリ | 75,688 KB |
実行使用メモリ | 72,288 KB |
最終ジャッジ日時 | 2023-09-09 05:37:37 |
合計ジャッジ時間 | 14,942 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge14 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 122 ms
55,712 KB |
testcase_01 | AC | 123 ms
55,952 KB |
testcase_02 | AC | 125 ms
55,816 KB |
testcase_03 | AC | 124 ms
56,032 KB |
testcase_04 | AC | 122 ms
55,720 KB |
testcase_05 | AC | 123 ms
55,732 KB |
testcase_06 | AC | 128 ms
55,564 KB |
testcase_07 | AC | 131 ms
56,048 KB |
testcase_08 | AC | 129 ms
55,828 KB |
testcase_09 | AC | 129 ms
56,036 KB |
testcase_10 | AC | 130 ms
55,460 KB |
testcase_11 | AC | 133 ms
56,052 KB |
testcase_12 | AC | 129 ms
55,804 KB |
testcase_13 | AC | 355 ms
60,836 KB |
testcase_14 | AC | 435 ms
60,548 KB |
testcase_15 | AC | 369 ms
60,400 KB |
testcase_16 | AC | 548 ms
64,036 KB |
testcase_17 | AC | 770 ms
70,144 KB |
testcase_18 | AC | 835 ms
70,820 KB |
testcase_19 | AC | 763 ms
64,916 KB |
testcase_20 | AC | 707 ms
66,896 KB |
testcase_21 | AC | 784 ms
65,112 KB |
testcase_22 | AC | 123 ms
56,044 KB |
testcase_23 | AC | 123 ms
55,884 KB |
testcase_24 | AC | 124 ms
56,072 KB |
testcase_25 | AC | 773 ms
69,392 KB |
testcase_26 | AC | 926 ms
68,488 KB |
testcase_27 | AC | 916 ms
72,288 KB |
testcase_28 | AC | 766 ms
66,724 KB |
ソースコード
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)); } }