結果

問題 No.417 チューリップバブル
ユーザー 14番14番
提出日時 2016-09-03 14:17:51
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 4,004 bytes
コンパイル時間 1,436 ms
コンパイル使用メモリ 116,704 KB
実行使用メモリ 92,608 KB
最終ジャッジ日時 2024-04-27 15:18:41
合計ジャッジ時間 5,153 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
32,596 KB
testcase_01 AC 35 ms
23,856 KB
testcase_02 AC 34 ms
26,032 KB
testcase_03 AC 36 ms
25,592 KB
testcase_04 AC 35 ms
27,808 KB
testcase_05 AC 34 ms
25,720 KB
testcase_06 AC 37 ms
25,728 KB
testcase_07 AC 35 ms
27,892 KB
testcase_08 TLE -
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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.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;
            }
        }

        if (!flags.Contains(false))
        {
            if (pos == 0)
            {
                return 0;
            }
        }

        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();
    }
}
0