結果
問題 | No.860 買い物 |
ユーザー | tenten |
提出日時 | 2020-08-31 22:38:50 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 939 ms / 1,000 ms |
コード長 | 2,075 bytes |
コンパイル時間 | 4,042 ms |
コンパイル使用メモリ | 74,988 KB |
実行使用メモリ | 73,728 KB |
最終ジャッジ日時 | 2023-08-10 17:59:03 |
合計ジャッジ時間 | 16,751 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge14 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 121 ms
55,916 KB |
testcase_01 | AC | 114 ms
55,772 KB |
testcase_02 | AC | 120 ms
55,664 KB |
testcase_03 | AC | 118 ms
56,260 KB |
testcase_04 | AC | 129 ms
55,620 KB |
testcase_05 | AC | 131 ms
56,136 KB |
testcase_06 | AC | 240 ms
60,332 KB |
testcase_07 | AC | 887 ms
71,380 KB |
testcase_08 | AC | 937 ms
71,320 KB |
testcase_09 | AC | 939 ms
71,152 KB |
testcase_10 | AC | 919 ms
71,732 KB |
testcase_11 | AC | 795 ms
71,128 KB |
testcase_12 | AC | 813 ms
73,728 KB |
testcase_13 | AC | 926 ms
71,436 KB |
testcase_14 | AC | 926 ms
71,640 KB |
testcase_15 | AC | 739 ms
71,316 KB |
testcase_16 | AC | 896 ms
71,396 KB |
testcase_17 | AC | 781 ms
71,356 KB |
ソースコード
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; } } }