典型90問    次の典型90問へ    前の典型90問へ

典型90問 032 AtCoder Ekiden(★3)


問題へのリンク


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");
            WillReturn.Add("1 10 100");
            WillReturn.Add("10 1 100");
            WillReturn.Add("100 10 1");
            WillReturn.Add("1");
            WillReturn.Add("1 2");
            //111
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("4");
            WillReturn.Add("1 2 3 4");
            WillReturn.Add("5 6 7 8");
            WillReturn.Add("9 10 11 12");
            WillReturn.Add("13 14 15 16");
            WillReturn.Add("3");
            WillReturn.Add("1 2");
            WillReturn.Add("1 3");
            WillReturn.Add("2 3");
            //-1
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("3");
            WillReturn.Add("1 10 100");
            WillReturn.Add("10 1 100");
            WillReturn.Add("100 10 1");
            WillReturn.Add("0");
            //3
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

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

        int N = int.Parse(InputList[0]);

        int[,] MatrixArr = CreateBanArr(InputList.Skip(1).Take(N));
        int UB = MatrixArr.GetUpperBound(0);

        int[] wkArr = { };
        Action<string> SplitAct = pStr =>
            wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();

        var NGPairSet = new HashSet<int>();
        foreach (string EachStr in InputList.Skip(1 + N + 1)) {
            SplitAct(EachStr);
            int X = wkArr[0];
            int Y = wkArr[1];

            int Hash1 = GetHash(X, Y);
            int Hash2 = GetHash(Y, X);
            NGPairSet.Add(Hash1);
            NGPairSet.Add(Hash2);
        }

        var BaseList = new List<int>();
        for (int I = 1; I <= N; I++) {
            BaseList.Add(I);
        }

        var AnswerKouho = new List<int>();
        foreach (int[] EachArr in RubyPatternClass<int>.Permutation(BaseList, N)) {

            bool IsNG = false;
            for (int I = 1; I <= EachArr.GetUpperBound(0); I++) {
                int PrevVal = EachArr[I - 1];
                int CurrVal = EachArr[I];

                int Hash = GetHash(PrevVal, CurrVal);
                if (NGPairSet.Contains(Hash)) {
                    IsNG = true;
                    break;
                }
            }
            if (IsNG) continue;

            int CostSum = 0;
            for (int I = 0; I <= EachArr.GetUpperBound(0); I++) {
                int CurrCost = MatrixArr[I, EachArr[I] - 1];
                CostSum += CurrCost;
            }
            AnswerKouho.Add(CostSum);
        }
        if (AnswerKouho.Count == 0) {
            Console.WriteLine(-1);
        }
        else {
            Console.WriteLine(AnswerKouho.Min());
        }
    }

    static int GetHash(int p1, int p2)
    {
        return p1 * 100 + p2;
    }

    ////////////////////////////////////////////////////////////////
    // IEnumerable<string>をintの2次元配列に設定する
    ////////////////////////////////////////////////////////////////
    static int[,] CreateBanArr(IEnumerable<string> pStrEnum)
    {
        var StrList = pStrEnum.ToList();
        if (StrList.Count == 0) {
            return new int[0, 0];
        }

        int[] IntArr = { };
        Action<string> SplitAct = pStr =>
            IntArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();

        SplitAct(StrList[0]);

        int UB_X = IntArr.GetUpperBound(0);
        int UB_Y = StrList.Count - 1;

        int[,] WillReturn = new int[UB_X + 1, UB_Y + 1];

        for (int Y = 0; Y <= UB_Y; Y++) {
            SplitAct(StrList[Y]);
            for (int X = 0; X <= UB_X; X++) {
                WillReturn[X, Y] = IntArr[X];
            }
        }
        return WillReturn;
    }
}

#region RubyPatternClass
// Rubyの場合の数
internal static class RubyPatternClass<Type>
{
    // 順列を返す
    private struct JyoutaiDef_Permutation
    {
        internal List<int> SelectedIndList;
    }
    internal static IEnumerable<Type[]> Permutation(IEnumerable<Type> pEnum, int pR)
    {
        if (pR == 0) yield break;
        Type[] pArr = pEnum.ToArray();
        if (pArr.Length < pR) yield break;

        var Stk = new Stack<JyoutaiDef_Permutation>();
        JyoutaiDef_Permutation WillPush;
        for (int I = pArr.GetUpperBound(0); 0 <= I; I--) {
            WillPush.SelectedIndList = new List<int>() { I };
            Stk.Push(WillPush);
        }

        while (Stk.Count > 0) {
            JyoutaiDef_Permutation Popped = Stk.Pop();

            // クリア判定
            if (Popped.SelectedIndList.Count == pR) {
                var WillReturn = new List<Type>();
                Popped.SelectedIndList.ForEach(X => WillReturn.Add(pArr[X]));
                yield return WillReturn.ToArray();
                continue;
            }

            for (int I = pArr.GetUpperBound(0); 0 <= I; I--) {
                if (Popped.SelectedIndList.Contains(I)) continue;
                WillPush.SelectedIndList = new List<int>(Popped.SelectedIndList) { I };
                Stk.Push(WillPush);
            }
        }
    }
}
#endregion


解説

順列を全て列挙してます。