結果
| 問題 |
No.860 買い物
|
| コンテスト | |
| ユーザー |
htensai
|
| 提出日時 | 2020-05-14 10:53:35 |
| 言語 | Java (openjdk 23) |
| 結果 |
TLE
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 1,854 bytes |
| コンパイル時間 | 2,563 ms |
| コンパイル使用メモリ | 77,840 KB |
| 実行使用メモリ | 70,048 KB |
| 最終ジャッジ日時 | 2024-09-15 00:55:10 |
| 合計ジャッジ時間 | 17,806 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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();
int[] prices = new int[n];
int[] costs = new int[n];
UnionFindTree uft = new UnionFindTree(n + 1);
PriorityQueue<Path> queue = new PriorityQueue<>();
long total = 0;
for (int i = 0; i < n; i++) {
int p = sc.nextInt();
total += p;
queue.add(new Path(i, n, p));
int c = sc.nextInt();
if (i > 0) {
queue.add(new Path(i - 1, i, c));
}
}
while (queue.size() > 0) {
Path p = queue.poll();
if (!uft.same(p)) {
uft.unite(p);
total += p.value;
}
}
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 (parents[x] == 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(y)] = find(x);
}
}
public void unite(Path p) {
unite(p.left, p.right);
}
}
static class Path implements Comparable<Path> {
int left;
int right;
int value;
public Path(int left, int right, int value) {
this.left = left;
this.right = right;
this.value = value;
}
public int compareTo(Path another) {
return value - another.value;
}
}
}
htensai