結果

問題 No.417 チューリップバブル
ユーザー tentententen
提出日時 2021-01-25 19:30:05
言語 Java21
(openjdk 21)
結果
AC  
実行時間 482 ms / 2,000 ms
コード長 2,077 bytes
コンパイル時間 4,519 ms
コンパイル使用メモリ 76,268 KB
実行使用メモリ 65,896 KB
最終ジャッジ日時 2023-09-04 15:51:54
合計ジャッジ時間 15,680 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
55,848 KB
testcase_01 AC 132 ms
55,844 KB
testcase_02 AC 128 ms
56,156 KB
testcase_03 AC 127 ms
56,348 KB
testcase_04 AC 128 ms
56,016 KB
testcase_05 AC 129 ms
55,560 KB
testcase_06 AC 130 ms
55,948 KB
testcase_07 AC 136 ms
55,804 KB
testcase_08 AC 145 ms
55,516 KB
testcase_09 AC 149 ms
55,856 KB
testcase_10 AC 145 ms
55,772 KB
testcase_11 AC 165 ms
56,012 KB
testcase_12 AC 172 ms
56,140 KB
testcase_13 AC 157 ms
55,952 KB
testcase_14 AC 180 ms
56,692 KB
testcase_15 AC 166 ms
56,224 KB
testcase_16 AC 171 ms
56,352 KB
testcase_17 AC 249 ms
58,992 KB
testcase_18 AC 244 ms
58,860 KB
testcase_19 AC 263 ms
59,620 KB
testcase_20 AC 284 ms
60,192 KB
testcase_21 AC 262 ms
59,276 KB
testcase_22 AC 286 ms
58,368 KB
testcase_23 AC 246 ms
58,756 KB
testcase_24 AC 178 ms
56,012 KB
testcase_25 AC 231 ms
59,820 KB
testcase_26 AC 219 ms
56,772 KB
testcase_27 AC 222 ms
58,784 KB
testcase_28 AC 254 ms
59,032 KB
testcase_29 AC 278 ms
59,512 KB
testcase_30 AC 263 ms
59,444 KB
testcase_31 AC 281 ms
59,848 KB
testcase_32 AC 128 ms
55,548 KB
testcase_33 AC 129 ms
55,872 KB
testcase_34 AC 146 ms
55,976 KB
testcase_35 AC 422 ms
63,956 KB
testcase_36 AC 482 ms
65,896 KB
testcase_37 AC 437 ms
63,372 KB
testcase_38 AC 443 ms
64,520 KB
testcase_39 AC 355 ms
58,792 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>();
    static int[] values;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        values = new int[n];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
            graph.add(new HashMap<>());
        }
        for (int i = 0; i < n - 1; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int c = sc.nextInt();
            graph.get(a).put(b, c);
            graph.get(b).put(a, c);
        }
        System.out.println(dfw(0, 0, m).lastEntry().getValue());
    }
    
    static TreeMap<Integer, Integer> dfw(int idx, int parent, int time) {
        graph.get(idx).remove(parent);
        TreeMap<Integer, Integer> tmp = new TreeMap<>();
        if (time < 0) {
            return tmp;
        }
        tmp.put(0, values[idx]);
        for (int x : graph.get(idx).keySet()) {
            int cost = graph.get(idx).get(x) * 2;
            TreeMap<Integer, Integer> next = dfw(x, idx, time - cost);
            Integer key = Integer.MAX_VALUE;
            while ((key = tmp.lowerKey(key)) != null) {
                for (int y : next.keySet()) {
                    if (cost + y + key > time) {
                        break;
                    }
                    int value = tmp.get(key) + next.get(y);
                    if (tmp.floorEntry(cost + y + key).getValue() < value) {
                        tmp.put(cost + y + key, value);
                        Integer mask = cost + y + key;
                        while ((mask = tmp.higherKey(mask)) != null) {
                            if (tmp.get(mask) <= value) {
                                tmp.remove(mask);
                            } else {
                                break;
                            }
                        }
                    }
                }
            }
        }
        return tmp;
    }
}
0