AtCoderのABC
次のABCの問題へ
前のABCの問題へ
ABC142-E Get Everything
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("2 3");
WillReturn.Add("10 1");
WillReturn.Add("1");
WillReturn.Add("15 1");
WillReturn.Add("2");
WillReturn.Add("30 2");
WillReturn.Add("1 2");
//25
}
else if (InputPattern == "Input2") {
WillReturn.Add("12 1");
WillReturn.Add("100000 1");
WillReturn.Add("2");
//-1
}
else if (InputPattern == "Input3") {
WillReturn.Add("4 6");
WillReturn.Add("67786 3");
WillReturn.Add("1 3 4");
WillReturn.Add("3497 1");
WillReturn.Add("2");
WillReturn.Add("44908 3");
WillReturn.Add("2 3 4");
WillReturn.Add("2156 3");
WillReturn.Add("2 3 4");
WillReturn.Add("26230 1");
WillReturn.Add("2");
WillReturn.Add("86918 1");
WillReturn.Add("3");
//69942
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
struct ACInfoDef
{
internal int A;
internal int BitOrC;
}
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 N = wkArr[0];
int CompleteBit = (1 << N) - 1;
var ACInfoList = new List<ACInfoDef>();
for (int I = 1; I <= InputList.Count - 1; I += 2) {
ACInfoDef WillAdd;
SplitAct(InputList[I]);
WillAdd.A = wkArr[0];
SplitAct(InputList[I + 1]);
int BitOrC = 0;
foreach (int EachC in wkArr) {
BitOrC += (1 << (EachC - 1));
}
WillAdd.BitOrC = BitOrC;
ACInfoList.Add(WillAdd);
}
// 最小コスト[空けれる宝箱のBitOr]なDP表
int?[] DPArr = new int?[CompleteBit + 1];
DPArr[0] = 0;
foreach (ACInfoDef EachACInfo in ACInfoList) {
for (int I = CompleteBit; 0 <= I; I--) {
if (DPArr[I].HasValue == false) continue;
int NewInd = I | EachACInfo.BitOrC;
int NewVal = DPArr[I].Value + EachACInfo.A;
if (DPArr[NewInd].HasValue) {
if (DPArr[NewInd].Value <= NewVal) {
continue;
}
}
DPArr[NewInd] = NewVal;
}
}
if (DPArr[CompleteBit].HasValue) {
Console.WriteLine(DPArr[CompleteBit]);
}
else {
Console.WriteLine(-1);
}
}
}
解説
BitDPで解いてます。