結果
問題 | No.417 チューリップバブル |
ユーザー | 14番 |
提出日時 | 2016-11-11 04:20:43 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 172 ms / 2,000 ms |
コード長 | 5,160 bytes |
コンパイル時間 | 1,951 ms |
コンパイル使用メモリ | 116,252 KB |
実行使用メモリ | 49,964 KB |
最終ジャッジ日時 | 2024-11-25 07:41:06 |
合計ジャッジ時間 | 6,055 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 39 ms
25,336 KB |
testcase_01 | AC | 40 ms
25,484 KB |
testcase_02 | AC | 39 ms
27,528 KB |
testcase_03 | AC | 38 ms
25,464 KB |
testcase_04 | AC | 35 ms
27,688 KB |
testcase_05 | AC | 37 ms
27,452 KB |
testcase_06 | AC | 39 ms
25,492 KB |
testcase_07 | AC | 38 ms
27,376 KB |
testcase_08 | AC | 46 ms
25,460 KB |
testcase_09 | AC | 39 ms
25,344 KB |
testcase_10 | AC | 52 ms
29,940 KB |
testcase_11 | AC | 44 ms
26,368 KB |
testcase_12 | AC | 45 ms
25,904 KB |
testcase_13 | AC | 42 ms
25,420 KB |
testcase_14 | AC | 67 ms
33,420 KB |
testcase_15 | AC | 45 ms
25,588 KB |
testcase_16 | AC | 46 ms
23,472 KB |
testcase_17 | AC | 63 ms
30,044 KB |
testcase_18 | AC | 64 ms
31,976 KB |
testcase_19 | AC | 63 ms
31,988 KB |
testcase_20 | AC | 96 ms
39,796 KB |
testcase_21 | AC | 120 ms
44,568 KB |
testcase_22 | AC | 84 ms
36,860 KB |
testcase_23 | AC | 94 ms
42,384 KB |
testcase_24 | AC | 37 ms
23,736 KB |
testcase_25 | AC | 90 ms
31,100 KB |
testcase_26 | AC | 49 ms
27,800 KB |
testcase_27 | AC | 60 ms
31,876 KB |
testcase_28 | AC | 139 ms
43,484 KB |
testcase_29 | AC | 151 ms
46,392 KB |
testcase_30 | AC | 115 ms
44,184 KB |
testcase_31 | AC | 150 ms
47,120 KB |
testcase_32 | AC | 36 ms
27,500 KB |
testcase_33 | AC | 37 ms
27,532 KB |
testcase_34 | AC | 39 ms
25,384 KB |
testcase_35 | AC | 172 ms
48,868 KB |
testcase_36 | AC | 157 ms
47,804 KB |
testcase_37 | AC | 115 ms
49,964 KB |
testcase_38 | AC | 101 ms
36,612 KB |
testcase_39 | AC | 61 ms
32,316 KB |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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(); } }