結果
| 問題 |
No.748 yuki国のお財布事情
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2020-09-01 18:04:52 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 1,405 ms / 2,000 ms |
| コード長 | 2,111 bytes |
| コンパイル時間 | 3,213 ms |
| コンパイル使用メモリ | 77,812 KB |
| 実行使用メモリ | 72,444 KB |
| 最終ジャッジ日時 | 2024-11-20 04:21:10 |
| 合計ジャッジ時間 | 20,252 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 26 |
ソースコード
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;
}
}
}
tenten