AtCoderのPAST    前のPASTの問題へ

第20回PAST N パワーアップ


問題へのリンク


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("3 5 2");
            WillReturn.Add("4 1");
            WillReturn.Add("1 7");
            WillReturn.Add("6 3");
            //11
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("3 500000000 500000000");
            WillReturn.Add("1 1000000000");
            WillReturn.Add("1000000000 1");
            WillReturn.Add("1000000000 1000000000");
            //2999999997
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("8 36 49");
            WillReturn.Add("73 52");
            WillReturn.Add("38 86");
            WillReturn.Add("30 52");
            WillReturn.Add("85 48");
            WillReturn.Add("27 60");
            WillReturn.Add("45 40");
            WillReturn.Add("65 98");
            WillReturn.Add("71 37");
            //228
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static long[] GetSplitArr(string pStr)
    {
        return (pStr == "" ? new string[0] : pStr.Split(' ')).Select(pX => long.Parse(pX)).ToArray();
    }

    struct PointDef
    {
        internal long X;
        internal long Y;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();

        long[] wkArr = { };
        Action<string> SplitAct = (pStr) => wkArr = GetSplitArr(pStr);

        SplitAct(InputList[0]);
        long N = wkArr[0];
        long A = wkArr[1];
        long B = wkArr[2];

        // 座標[2べきの値]なDict
        var PosDict = new Dictionary<long, PointDef>();

        long CurrBeki2 = 1;
        PosDict[CurrBeki2] = new PointDef() { X = 1, Y = 1 };
        CurrBeki2 *= 2;
        PosDict[CurrBeki2] = new PointDef() { X = B, Y = A };

        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);
            CurrBeki2 *= 2;
            PosDict[CurrBeki2] = new PointDef() { X = wkArr[1], Y = wkArr[0] };
        }

        long AllBitOn = PosDict.Keys.Sum();
        long[] KeysArr = PosDict.Keys.ToArray();
        long KeysArr_UB = KeysArr.GetUpperBound(0);

        // 最小コスト[訪問済BitSet , 現在位置]なインラインDP表
        long?[,] DPArr = new long?[AllBitOn + 1, KeysArr_UB + 1];
        DPArr[1, 0] = 0;

        // 数値[PopCount]なDict
        var ListDict = new Dictionary<long, List<long>>();
        for (int I = 0; I <= AllBitOn; I++) {
            long CurrPopCount = PopCount(I);
            if (ListDict.ContainsKey(CurrPopCount) == false) {
                ListDict[CurrPopCount] = new List<long>();
            }
            ListDict[CurrPopCount].Add(I);
        }

        for (long I = 1; I <= KeysArr.Length; I++) {
            foreach (long J in ListDict[I - 1]) {
                for (long K = 0; K <= KeysArr_UB; K++) {
                    if (DPArr[J, K].HasValue == false) continue;
                    long BitK = KeysArr[K];

                    // 8近傍に移動可能か?
                    bool CanMove8 = false;
                    if ((J & 2) > 0) {
                        CanMove8 = true;
                    }

                    // 移動先でループ
                    for (long L = 0; L <= KeysArr_UB; L++) {
                        long BitL = KeysArr[L];

                        // 最訪は不可
                        if ((J & BitL) > 0) continue;

                        long NewJ = J | BitL;
                        long NewK = L;
                        PointDef Pos1 = PosDict[BitK];
                        PointDef Pos2 = PosDict[BitL];

                        long NewVal = DPArr[J, K].Value + DeriveKyori(Pos1, Pos2, CanMove8);

                        if (DPArr[NewJ, NewK].HasValue) {
                            if (DPArr[NewJ, NewK] <= NewVal) {
                                continue;
                            }
                        }
                        DPArr[NewJ, NewK] = NewVal;
                    }
                }
            }
        }

        var AnswerList = new List<long>();
        long GoalBit = AllBitOn;
        GoalBit -= 2; // パワーアップは必須ではない
        GoalBit -= 1; // スタート座標
        for (long J = AllBitOn; 0 <= J; J--) {
            for (long K = 0; K <= KeysArr_UB; K++) {
                if (DPArr[J, K].HasValue == false) continue;

                if ((GoalBit & J) == GoalBit) {
                    AnswerList.Add(DPArr[J, K].Value);
                }
            }
        }
        Console.WriteLine(AnswerList.Min());
    }

    // 座標2つと、8近傍に移動可能かを引数とし、距離を返す
    static long DeriveKyori(PointDef pPos1, PointDef pPos2, bool pCanMove8)
    {
        long X1 = pPos1.X;
        long Y1 = pPos1.Y;
        long X2 = pPos2.X;
        long Y2 = pPos2.Y;

        long DiffX = Math.Abs(X1 - X2);
        long DiffY = Math.Abs(Y1 - Y2);

        if (pCanMove8 == false) {
            return DiffX + DiffY;
        }
        return Math.Max(DiffX, DiffY);
    }

    ////////////////////////////////////////////////////////////////
    // C++のPopCount
    ////////////////////////////////////////////////////////////////
    static long PopCount(long pVal)
    {
        long WillReturn = 0;
        while (pVal > 0) {
            if (pVal % 2 == 1) WillReturn++;
            pVal /= 2;
        }
        return WillReturn;
    }
}


解説

TSP問題ですので、
最小コスト[訪問済BitSet , 現在位置]なインラインDP表でBitDPしてます。
(訪問済BitSetは、遷移で必ず増えるので、インラインDPにできます)

数値[PopCount]なDictを前処理で作成し、高速化もしてます。