結果
| 問題 |
No.417 チューリップバブル
|
| コンテスト | |
| ユーザー |
14番
|
| 提出日時 | 2016-09-03 14:11:24 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,867 bytes |
| コンパイル時間 | 3,414 ms |
| コンパイル使用メモリ | 116,080 KB |
| 実行使用メモリ | 119,464 KB |
| 最終ジャッジ日時 | 2024-11-15 18:35:37 |
| 合計ジャッジ時間 | 90,464 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 11 TLE * 29 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
class Program
{
public void Proc()
{
Reader.IsDebug = false;
int[] inpt = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray();
this.MuraCount = inpt[0];
this.TimeLimit = inpt[1];
this.Zeisyu = new long[this.MuraCount + 1];
for (int i = 0; i < MuraCount; i++)
{
this.Zeisyu[i] = long.Parse(Reader.ReadLine());
}
for (int i = 0; i < MuraCount - 1; i++)
{
inpt = Reader.ReadLine().Split(' ').Select(a=>int.Parse(a)).ToArray();
int m1 = inpt[0];
int m2 = inpt[1];
int cost = inpt[2];
if (!this.Road.ContainsKey(m1))
{
this.Road.Add(m1, new Dictionary<int, int>());
}
this.Road[m1][m2] = cost;
if(!this.Road.ContainsKey(m2)) {
this.Road.Add(m2, new Dictionary<int,int>());
}
this.Road[m2][m1] = cost;
}
bool[] flags = new bool[MuraCount];
flags[0] = true;
long ans = Zeisyu[0];
ans = Math.Max(ans, this.GetAns(0, TimeLimit, flags) + Zeisyu[0]);
Console.WriteLine(ans);
}
private Dictionary<int, Dictionary<int, Dictionary<string, long>>> dic = new Dictionary<int, Dictionary<int, Dictionary<string, long>>>();
private long GetAns(int pos, int remainTime, bool[] flags)
{
string key = string.Join(string.Empty, flags.Select(a => a ? 1 : 0));
if (!dic.ContainsKey(pos))
{
dic.Add(pos, new Dictionary<int, Dictionary<string, long>>());
}
if (!dic[pos].ContainsKey(remainTime))
{
dic[pos].Add(remainTime, new Dictionary<string, long>());
}
if (dic[pos][remainTime].ContainsKey(key))
{
return dic[pos][remainTime][key];
}
if (remainTime < 0)
{
return -1;
}
if (remainTime == 0)
{
if (pos == 0)
{
return 0;
} else {
return -1;
}
}
long ans = -1;
if (pos == 0)
{
ans = 0;
}
if (dic.ContainsKey(pos))
{
bool canGetTax = !flags[pos];
flags[pos] = true;
foreach (int nextPos in Road[pos].Keys)
{
long ret = this.GetAns(nextPos, remainTime - Road[pos][nextPos], flags);
if (ret >= 0)
{
if (canGetTax)
{
ret += Zeisyu[pos];
}
ans = Math.Max(ans, ret);
}
}
if (canGetTax)
{
flags[pos] = false;
}
}
dic[pos][remainTime].Add(key, ans);
return ans;
}
private Dictionary<int, Dictionary<int, int>> Road = new Dictionary<int, Dictionary<int, int>>();
private int MuraCount;
private int TimeLimit;
long[] Zeisyu;
public class Reader
{
public static bool IsDebug = true;
private static String PlainInput = @"
2 50
10
50
0 1 50
";
private static System.IO.StringReader Sr = null;
public static string ReadLine()
{
if (IsDebug)
{
if (Sr == null)
{
Sr = new System.IO.StringReader(PlainInput.Trim());
}
return Sr.ReadLine();
}
else
{
return Console.ReadLine();
}
}
}
static void Main()
{
Program prg = new Program();
prg.Proc();
}
}
14番