AtCoderのABC    次のABCの問題へ    前のABCの問題へ

ABC244-F Shortest Good Path


問題へのリンク


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 2");
            WillReturn.Add("1 2");
            WillReturn.Add("2 3");
            //14
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("5 5");
            WillReturn.Add("4 2");
            WillReturn.Add("2 3");
            WillReturn.Add("1 3");
            WillReturn.Add("2 1");
            WillReturn.Add("1 5");
            //108
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static int mN;

    // 隣接リスト
    static Dictionary<int, List<int>> mToNodeListDict = new Dictionary<int, List<int>>();

    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]);
        mN = wkArr[0];

        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);
            int FromNode = wkArr[0];
            int ToNode = wkArr[1];

            if (mToNodeListDict.ContainsKey(FromNode) == false) {
                mToNodeListDict[FromNode] = new List<int>();
            }
            if (mToNodeListDict.ContainsKey(ToNode) == false) {
                mToNodeListDict[ToNode] = new List<int>();
            }
            mToNodeListDict[FromNode].Add(ToNode);
            mToNodeListDict[ToNode].Add(FromNode);
        }

        ExecBFS();
    }

    struct JyoutaiDef
    {
        internal int CurrNode;
        internal int Level;
        internal int VisitedBitSet;
    }

    static void ExecBFS()
    {
        var Que = new Queue<JyoutaiDef>();
        JyoutaiDef WillEnqueue;

        var AnswerDict = new Dictionary<int, int>();
        AnswerDict[0] = 0;

        var VisitedSet = new HashSet<int>();

        for (int I = 1; I <= mN; I++) {
            WillEnqueue.CurrNode = I;
            WillEnqueue.Level = 1;
            WillEnqueue.VisitedBitSet = 1 << (I - 1);
            Que.Enqueue(WillEnqueue);
            AnswerDict[WillEnqueue.VisitedBitSet] = 1;

            int Hash = GetHash(WillEnqueue);
            VisitedSet.Add(Hash);
        }

        while (Que.Count > 0) {
            JyoutaiDef Dequeued = Que.Dequeue();

            if (mToNodeListDict.ContainsKey(Dequeued.CurrNode)) {
                foreach (int EachToNode in mToNodeListDict[Dequeued.CurrNode]) {
                    WillEnqueue.CurrNode = EachToNode;
                    WillEnqueue.Level = Dequeued.Level + 1;
                    WillEnqueue.VisitedBitSet = Dequeued.VisitedBitSet;

                    int CurrBit = 1 << (EachToNode - 1);
                    WillEnqueue.VisitedBitSet ^= CurrBit;
                    int Hash = GetHash(WillEnqueue);
                    if (VisitedSet.Add(Hash) == false) {
                        continue;
                    }

                    if (AnswerDict.ContainsKey(WillEnqueue.VisitedBitSet) == false
                        || AnswerDict[WillEnqueue.VisitedBitSet] > WillEnqueue.Level) {
                        AnswerDict[WillEnqueue.VisitedBitSet] = WillEnqueue.Level;
                    }
                    Que.Enqueue(WillEnqueue);
                }
            }
        }
        Console.WriteLine(AnswerDict.Values.Sum());
    }

    static int GetHash(JyoutaiDef pJyoutai)
    {
        return pJyoutai.CurrNode * 1000000 + pJyoutai.VisitedBitSet;
    }
}


解説

登場ノードごとの偶奇の全ての組み合わせごとの
最短のパス長がほしいので、
BFSで全てのノードから全探索してます。