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

ABC317-C Remembering the Days


問題へのリンク


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

    static int mN;

    struct EdgeInfoDef
    {
        internal int ToNode;
        internal int Cost;
    }
    static Dictionary<int, List<EdgeInfoDef>> mEdgeInfoListDict = new Dictionary<int, List<EdgeInfoDef>>();

    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 A = wkArr[0];
            int B = wkArr[1];
            int C = wkArr[2];

            Action<int, int, int> AddEdgeAct = (pFromNode, pToNode, pCost) =>
            {
                if (mEdgeInfoListDict.ContainsKey(pFromNode) == false) {
                    mEdgeInfoListDict[pFromNode] = new List<EdgeInfoDef>();
                }
                EdgeInfoDef WillAdd;
                WillAdd.ToNode = pToNode;
                WillAdd.Cost = pCost;
                mEdgeInfoListDict[pFromNode].Add(WillAdd);
            };
            AddEdgeAct(A, B, C);
            AddEdgeAct(B, A, C);
        }

        var AnswerKouho = new List<int>();
        for (int I = 1; I <= mN; I++) {
            AnswerKouho.Add(ExecDFS(I));
        }
        Console.WriteLine(AnswerKouho.Max());
    }

    struct JyoutaiDef
    {
        internal int CurrNode;
        internal int CostSum;
        internal int VisitedSet;
    }

    static int ExecDFS(int pStaNode)
    {
        var AnswerKouho = new List<int>();
        AnswerKouho.Add(0);

        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.CurrNode = pStaNode;
        WillPush.CostSum = 0;
        WillPush.VisitedSet = 1 << (pStaNode - 1);

        Stk.Push(WillPush);

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

            AnswerKouho.Add(Popped.CostSum);

            if (mEdgeInfoListDict.ContainsKey(Popped.CurrNode) == false) {
                continue;
            }
            foreach (EdgeInfoDef EachEdgeInfo in mEdgeInfoListDict[Popped.CurrNode]) {
                int Currbit = (1 << (EachEdgeInfo.ToNode - 1));
                if ((Popped.VisitedSet & Currbit) > 0) continue;

                WillPush.CurrNode = EachEdgeInfo.ToNode;
                WillPush.CostSum = Popped.CostSum + EachEdgeInfo.Cost;
                WillPush.VisitedSet = Popped.VisitedSet + Currbit;
                Stk.Push(WillPush);
            }
        }
        return AnswerKouho.Max();
    }
}


解説

訪問済ノードをBitに持ってDFSしてます。