結果

問題 No.417 チューリップバブル
ユーザー 14番14番
提出日時 2016-11-11 04:20:43
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 178 ms / 2,000 ms
コード長 5,160 bytes
コンパイル時間 1,054 ms
コンパイル使用メモリ 110,592 KB
実行使用メモリ 49,392 KB
最終ジャッジ日時 2024-05-04 01:51:50
合計ジャッジ時間 5,073 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
25,744 KB
testcase_01 AC 36 ms
25,460 KB
testcase_02 AC 37 ms
25,488 KB
testcase_03 AC 37 ms
27,784 KB
testcase_04 AC 33 ms
23,348 KB
testcase_05 AC 35 ms
25,520 KB
testcase_06 AC 38 ms
25,596 KB
testcase_07 AC 40 ms
25,600 KB
testcase_08 AC 41 ms
25,984 KB
testcase_09 AC 38 ms
25,960 KB
testcase_10 AC 49 ms
27,784 KB
testcase_11 AC 43 ms
28,664 KB
testcase_12 AC 44 ms
25,980 KB
testcase_13 AC 44 ms
25,684 KB
testcase_14 AC 72 ms
31,764 KB
testcase_15 AC 47 ms
25,596 KB
testcase_16 AC 50 ms
25,596 KB
testcase_17 AC 62 ms
30,044 KB
testcase_18 AC 63 ms
29,808 KB
testcase_19 AC 66 ms
30,060 KB
testcase_20 AC 96 ms
37,752 KB
testcase_21 AC 113 ms
44,560 KB
testcase_22 AC 81 ms
36,856 KB
testcase_23 AC 95 ms
40,600 KB
testcase_24 AC 35 ms
27,688 KB
testcase_25 AC 89 ms
33,412 KB
testcase_26 AC 49 ms
27,672 KB
testcase_27 AC 63 ms
33,908 KB
testcase_28 AC 133 ms
47,580 KB
testcase_29 AC 157 ms
48,696 KB
testcase_30 AC 113 ms
42,396 KB
testcase_31 AC 144 ms
49,028 KB
testcase_32 AC 38 ms
25,664 KB
testcase_33 AC 42 ms
27,920 KB
testcase_34 AC 41 ms
27,704 KB
testcase_35 AC 178 ms
49,392 KB
testcase_36 AC 166 ms
45,744 KB
testcase_37 AC 118 ms
43,704 KB
testcase_38 AC 101 ms
36,720 KB
testcase_39 AC 60 ms
30,280 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;

public class Program
{
    public void Proc() {

        Reader.IsDebug = false;
        int[] inpt = Reader.ReadLine().Split(' ').Select(a=>int.Parse(a)).ToArray();
        
        this.Map = new TreeNode[inpt[0]];

        this.TimeLimit = inpt[1];

        for(int i=0; i<this.Map.Length; i++) {
            this.Map[i] = new TreeNode(i, long.Parse(Reader.ReadLine()));
        }

        for(int i=0; i<this.Map.Length-1; i++) {
            inpt = Reader.ReadLine().Split(' ').Select(a=>int.Parse(a)).ToArray();
            int[][] posList = new int[][]{new int[]{inpt[0], inpt[1]}, new int[] {inpt[1], inpt[0]}};
            foreach(int[] pos in posList) {
                if(!this.Road.ContainsKey(pos[0])) {
                    this.Road.Add(pos[0], new Dictionary<int, int>());
                }
                this.Road[pos[0]][pos[1]] = inpt[2];
            }
        }

        TreeNode root = this.Map[0];
        SetUpTree(root);
        SetCost(root);
        Console.WriteLine(root.CostDic.Max(a=>a.Value));
    }

    private void SetCost(TreeNode t) {
        if(t.ToRootCost * 2 > TimeLimit) {
            return;
        }
        List<Dictionary<int, long>> dicList = new List<Dictionary<int, long>>();
        foreach(TreeNode sub in t.Items) {
            SetCost(sub);
            Dictionary<int, long> tmp = new Dictionary<int, long>();
            sub.CostDic.ToList().ForEach(a=>tmp.Add(a.Key + (sub.ToParentCost*2), a.Value));
            if(tmp.Count > 0) {
                dicList.Add(tmp);
            }
        }
        this.MargeDic.Clear();
        t.CostDic.Add(0, t.Tax);
        Dictionary<int, long> marged = this.Marge(dicList, 0);
        marged.Where(a=>a.Key <= TimeLimit - (t.ToRootCost * 2)).ToList().ForEach(a=>{
            if(t.CostDic.ContainsKey(a.Key)) {
                t.CostDic[a.Key] = Math.Max(t.CostDic[a.Key], a.Value + t.Tax);
            } else {
                t.CostDic.Add(a.Key, a.Value + t.Tax);
            }
        });
    }

    private Dictionary<int, Dictionary<int, long>> MargeDic = new Dictionary<int, Dictionary<int, long>>();
    private Dictionary<int, long> Marge(List<Dictionary<int, long>> list, int idx) {
        
        if(MargeDic.ContainsKey(idx)) {
            return MargeDic[idx];
        }
        if(idx == list.Count-1) {
            return list[idx];
        }

        if(list.Count <= 0) {
            return new Dictionary<int, long>();
        }

        Dictionary<int, long> ans = new Dictionary<int, long>();
        list[idx].ToList().ForEach(a=>ans.Add(a.Key, a.Value));

        Dictionary<int, long> ret = Marge(list, idx+1);
        ret.ToList().ForEach(a=>{
            if(ans.ContainsKey(a.Key)) {
                ans[a.Key] = Math.Max(ans[a.Key], a.Value);
            } else {
                ans.Add(a.Key, a.Value);
            }
            list[idx].ToList().ForEach(b=>{
                int newKey = a.Key + b.Key;
                long newVal = a.Value + b.Value;
                if(ans.ContainsKey(newKey)) {
                    ans[newKey] = Math.Max(ans[newKey], newVal);
                } else {
                    ans.Add(newKey, newVal);
                }
            });
        });
        MargeDic[idx] = ans;
        return ans;
    }
    private void SetUpTree(TreeNode t) {
        foreach(int subTreeId in this.Road[t.ID].Keys) {
            if(t.Parent != null && subTreeId == t.Parent.ID) {
                continue;
            }
            t.AddItem(this.Map[subTreeId], this.Road[t.ID][subTreeId]);
            SetUpTree(this.Map[subTreeId]);
        }   
    }

    private Dictionary<int, Dictionary<int, int>> Road = new Dictionary<int, Dictionary<int, int>>();

    public int TimeLimit = 0;

    public TreeNode[] Map;

    public class TreeNode {
        public int ID;
        public long Tax;

        public int ToRootCost;
        public int ToParentCost;
        public TreeNode Parent = null;
        public List<TreeNode> Items = new List<TreeNode>();

        public Dictionary<int, long> CostDic = new Dictionary<int, long>();

        public TreeNode(int id, long tax) {
            this.ID = id;
            this.Tax = tax;
        }

        public void AddItem(TreeNode subTree, int Cost) {
            subTree.Parent = this;
            this.Items.Add(subTree);        
            subTree.ToParentCost = Cost;
            subTree.ToRootCost = this.ToRootCost + Cost;    
        }
    }



    public class Reader {
        public static bool IsDebug = true;
        private static System.IO.StringReader SReader;
        private static string InitText = @"





";
        public static string ReadLine() {
            if(IsDebug) {
                if(SReader == null) {
                    SReader = new System.IO.StringReader(InitText.Trim());
                }
                return SReader.ReadLine();
            } else {
                return Console.ReadLine();
            }
        }
    }
    public static void Main(string[] args)
    {
        Program prg = new Program();
        prg.Proc();
    }
}
0