結果

問題 No.860 買い物
ユーザー tentententen
提出日時 2022-08-25 10:17:34
言語 Java21
(openjdk 21)
結果
AC  
実行時間 580 ms / 1,000 ms
コード長 2,739 bytes
コンパイル時間 2,376 ms
コンパイル使用メモリ 79,248 KB
実行使用メモリ 58,244 KB
最終ジャッジ日時 2024-04-20 20:05:07
合計ジャッジ時間 9,400 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
36,864 KB
testcase_01 AC 47 ms
37,052 KB
testcase_02 AC 48 ms
37,104 KB
testcase_03 AC 46 ms
36,964 KB
testcase_04 AC 50 ms
37,220 KB
testcase_05 AC 49 ms
36,784 KB
testcase_06 AC 135 ms
40,684 KB
testcase_07 AC 563 ms
54,512 KB
testcase_08 AC 566 ms
54,104 KB
testcase_09 AC 565 ms
54,948 KB
testcase_10 AC 580 ms
54,224 KB
testcase_11 AC 365 ms
53,876 KB
testcase_12 AC 353 ms
58,244 KB
testcase_13 AC 564 ms
54,464 KB
testcase_14 AC 556 ms
54,212 KB
testcase_15 AC 373 ms
55,568 KB
testcase_16 AC 552 ms
53,960 KB
testcase_17 AC 349 ms
53,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        long total = 0;
        ArrayList<Path> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            int c = sc.nextInt();
            int d = sc.nextInt();
            if (i > 0) {
                list.add(new Path(i - 1, i, d));
            }
            list.add(new Path(i, n, c));
            total += c;
        }
        Collections.sort(list);
        UnionFindTree uft = new UnionFindTree(n + 1);
        for (Path x : list) {
            if (!uft.same(x)) {
                uft.unite(x);
                total += x.value;
            }
        }
        System.out.println(total);
    }
    
    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;
        }
    }
    
    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 x) {
            return same(x.left, x.right);
        }
        
        public void unite(int x, int y) {
            parents[find(x)] = find(y);
        }
        
        public void unite(Path x) {
            unite(x.left, x.right);
        }
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0