結果

問題 No.417 チューリップバブル
ユーザー htensaihtensai
提出日時 2020-01-28 16:44:23
言語 Java21
(openjdk 21)
結果
AC  
実行時間 609 ms / 2,000 ms
コード長 2,424 bytes
コンパイル時間 3,166 ms
コンパイル使用メモリ 75,760 KB
実行使用メモリ 58,284 KB
最終ジャッジ日時 2023-10-13 17:52:21
合計ジャッジ時間 16,660 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
49,380 KB
testcase_01 AC 46 ms
49,348 KB
testcase_02 AC 47 ms
49,468 KB
testcase_03 AC 47 ms
49,132 KB
testcase_04 AC 47 ms
49,684 KB
testcase_05 AC 45 ms
49,316 KB
testcase_06 AC 49 ms
49,788 KB
testcase_07 AC 48 ms
49,220 KB
testcase_08 AC 135 ms
54,224 KB
testcase_09 AC 239 ms
56,040 KB
testcase_10 AC 234 ms
55,336 KB
testcase_11 AC 305 ms
56,620 KB
testcase_12 AC 230 ms
56,172 KB
testcase_13 AC 242 ms
55,476 KB
testcase_14 AC 331 ms
55,728 KB
testcase_15 AC 120 ms
52,464 KB
testcase_16 AC 166 ms
54,744 KB
testcase_17 AC 300 ms
56,912 KB
testcase_18 AC 270 ms
55,448 KB
testcase_19 AC 301 ms
56,624 KB
testcase_20 AC 367 ms
56,424 KB
testcase_21 AC 329 ms
56,504 KB
testcase_22 AC 419 ms
56,420 KB
testcase_23 AC 416 ms
56,248 KB
testcase_24 AC 49 ms
49,308 KB
testcase_25 AC 326 ms
56,444 KB
testcase_26 AC 239 ms
56,376 KB
testcase_27 AC 289 ms
56,228 KB
testcase_28 AC 363 ms
56,440 KB
testcase_29 AC 420 ms
56,460 KB
testcase_30 AC 415 ms
56,360 KB
testcase_31 AC 423 ms
56,480 KB
testcase_32 AC 45 ms
49,308 KB
testcase_33 AC 209 ms
55,436 KB
testcase_34 AC 253 ms
55,288 KB
testcase_35 AC 609 ms
57,860 KB
testcase_36 AC 605 ms
57,900 KB
testcase_37 AC 529 ms
58,284 KB
testcase_38 AC 535 ms
56,636 KB
testcase_39 AC 539 ms
58,068 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static int m;
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] first = br.readLine().split(" ", 2);
        int n = Integer.parseInt(first[0]);
        m = Integer.parseInt(first[1]) + 1;
        Node[] tree = new Node[n];
        for (int i = 0; i < n; i++) {
            tree[i] = new Node(i, Integer.parseInt(br.readLine()));
        }
        for (int i = 0; i < n - 1; i++) {
            String[] line = br.readLine().split(" ", 3);
            int a = Integer.parseInt(line[0]);
            int b = Integer.parseInt(line[1]);
            int c = Integer.parseInt(line[2]);
            tree[a].add(tree[b], c);
            tree[b].add(tree[a], c);
        }
        int[] arr = tree[0].getArray(null);
        Arrays.sort(arr);
        System.out.println(arr[m - 1]);
  }
  
  static void printArrray(int[] arr) {
      StringBuilder sb = new StringBuilder();
      for (int x : arr) {
          sb.append(x).append(" ");
      }
      System.out.println(sb);
  }
   
   static class Node {
       int id;
       HashMap<Node, Integer> paths = new HashMap<>();
       int[] costs = new int[m];
       
       public Node(int id, int cost) {
           this.id = id;
           costs[0] = cost;
       }
       
       public void add(Node n, int cost) {
           paths.put(n, cost);
       }
       
       public int[] getArray(Node parent) {
           if (parent != null) {
                paths.remove(parent);
           }
           for (Map.Entry<Node, Integer> p : paths.entrySet()) {
               int[] tmp = new int[m];
               int[] child = p.getKey().getArray(this);
               int added = p.getValue() * 2;
               for (int i = 0; i < m - added; i++) {
                   for (int j = 0; j < m - i - added; j++) {
                       tmp[i + j + added] = Math.max(tmp[i + j + added] , costs[i] + child[j]);
                   }
               }
               for (int i = 0; i < m; i++) {
                   costs[i] = Math.max(costs[i], tmp[i]);
               }
           }
           return costs;
       }
       
       public int hashCode() {
           return id;
       }
       
       public boolean equals(Object o) {
           Node n = (Node) o;
           return id == n.id;
       }
   }
}
0