結果
| 問題 |
No.860 買い物
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2020-08-31 22:38:50 |
| 言語 | Java (openjdk 23) |
| 結果 |
TLE
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 2,075 bytes |
| コンパイル時間 | 2,742 ms |
| コンパイル使用メモリ | 78,300 KB |
| 実行使用メモリ | 65,800 KB |
| 最終ジャッジ日時 | 2024-11-17 02:40:38 |
| 合計ジャッジ時間 | 16,812 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 8 TLE * 7 |
ソースコード
import java.util.*;
public class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
PriorityQueue<Path> queue = new PriorityQueue<>();
long total = 0;
int first = sc.nextInt();
total += first;
queue.add(new Path(0, n, first));
sc.nextInt();
for (int i = 1; i < n; i++) {
int each = sc.nextInt();
total += each;
queue.add(new Path(i, n, each));
queue.add(new Path(i - 1, i, sc.nextInt()));
}
UnionFindTree uft = new UnionFindTree(n + 1);
while (queue.size() > 0) {
Path p = queue.poll();
if (!uft.same(p)) {
uft.unite(p);
total += p.cost;
}
}
System.out.println(total);
}
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(Path p) {
return same(p.left, p.right);
}
public void unite(int x, int y) {
if (!same(x, y)) {
parents[find(x)] = find(y);
}
}
public void unite(Path p) {
unite(p.left, p.right);
}
}
static class Path implements Comparable<Path> {
int left;
int right;
int cost;
public Path(int left, int right, int cost) {
this.left = left;
this.right = right;
this.cost = cost;
}
public int compareTo(Path another) {
return cost - another.cost;
}
}
}
tenten