DPコンテスト    次のDPコンテストの問題へ    前のDPコンテストの問題へ

TDP H ナップザック


問題へのリンク


C#のソース

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

class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("4 5 2");
            WillReturn.Add("1 10 1");
            WillReturn.Add("1 20 2");
            WillReturn.Add("1 30 3");
            WillReturn.Add("10 100 4");
            //50
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("10 20 2");
            WillReturn.Add("4 5 6");
            WillReturn.Add("3 3 9");
            WillReturn.Add("5 2 9");
            WillReturn.Add("4 1 6");
            WillReturn.Add("6 8 3");
            WillReturn.Add("3 7 6");
            WillReturn.Add("2 4 9");
            WillReturn.Add("4 7 3");
            WillReturn.Add("6 5 6");
            WillReturn.Add("3 2 9");
            //27
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct WVCInfoDef
    {
        internal int Weight;
        internal int Value;
        internal int Color;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        int[] wkArr = { };
        Action<string> SplitAct = pStr =>
            wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();

        SplitAct(InputList[0]);
        int W = wkArr[1];
        int C = wkArr[2];

        var WVCInfoList = new List<WVCInfoDef>();
        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);
            WVCInfoDef WillAdd;
            WillAdd.Weight = wkArr[0];
            WillAdd.Value = wkArr[1];
            WillAdd.Color = wkArr[2];
            WVCInfoList.Add(WillAdd);
        }
        WVCInfoDef[] WVCInfoArr = WVCInfoList.OrderBy(pX => pX.Color).ToArray();

        int UB_K = WVCInfoArr.Max(pX => pX.Color) + 1;

        // 最大価値[重さ合計 , 色合計 , 前の色] なインラインDP表
        int?[, ,] DPArr = new int?[W + 1, C + 1, UB_K + 1];
        DPArr[0, 0, 0] = 0;

        int RunSum = 0;
        var AppearColorSet = new HashSet<int>();
        int LimitJ = 0;
        int Answer = int.MinValue;
        foreach (WVCInfoDef EachWVCInfo in WVCInfoArr) {
            RunSum += EachWVCInfo.Weight;
            RunSum = Math.Min(RunSum, W);

            AppearColorSet.Add(EachWVCInfo.Color);
            LimitJ = Math.Min(C, AppearColorSet.Count);

            for (int I = RunSum; 0 <= I; I--) {
                for (int J = 0; J <= LimitJ; J++) {
                    for (int K = 0; K <= EachWVCInfo.Color; K++) {
                        if (DPArr[I, J, K].HasValue == false) continue;

                        int NewI = I + EachWVCInfo.Weight;
                        if (NewI > W) continue;

                        int NewJ = J;
                        if (K != EachWVCInfo.Color) NewJ++;
                        if (NewJ > C) continue;

                        int NewK = EachWVCInfo.Color;
                        int NewVal = DPArr[I, J, K].Value + EachWVCInfo.Value;
                        if (DPArr[NewI, NewJ, NewK].HasValue) {
                            if (DPArr[NewI, NewJ, NewK] >= NewVal) {
                                continue;
                            }
                        }
                        DPArr[NewI, NewJ, NewK] = NewVal;
                        Answer = Math.Max(Answer, NewVal);
                    }
                }
            }
        }
        Console.WriteLine(Answer);
    }
}


解説

最大価値[重さ合計 , 色合計 , 前の色] なインラインDP
でTLE対策で、枝切りを多くしてます。