結果
| 問題 |
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 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 40 |
コンパイルメッセージ
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();
}
}
14番