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

ABC361-E Tree and Hamilton Path 2


問題へのリンク


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

    struct EdgeInfoDef
    {
        internal long ToNode;
        internal long Cost;
    }

    static long mN;
    static Dictionary<long, List<EdgeInfoDef>> mEdgeInfoListDict = new Dictionary<long, List<EdgeInfoDef>>();

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

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

        mN = long.Parse(InputList[0]);

        long SumCost = 0;

        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);
            long S = wkArr[0];
            long T = wkArr[1];
            long W = wkArr[2];
            SumCost += W;

            if (mEdgeInfoListDict.ContainsKey(S) == false) {
                mEdgeInfoListDict[S] = new List<EdgeInfoDef>();
            }
            if (mEdgeInfoListDict.ContainsKey(T) == false) {
                mEdgeInfoListDict[T] = new List<EdgeInfoDef>();
            }
            mEdgeInfoListDict[S].Add(new EdgeInfoDef { ToNode = T, Cost = W });
            mEdgeInfoListDict[T].Add(new EdgeInfoDef { ToNode = S, Cost = W });
        }

        long wkLeafNode, wkMaxSumCost;
        ExecDFS(1, out wkLeafNode, out wkMaxSumCost);
        ExecDFS(wkLeafNode, out wkLeafNode, out wkMaxSumCost);
        Console.WriteLine(SumCost * 2 - wkMaxSumCost);
    }

    struct JyoutaiDef
    {
        internal long CurrNode;
        internal long SumCost;
    }

    // 開始ノードを引数として深さ優先探索を行い、最長の葉ノードと最大コストを求める
    static void ExecDFS(long pStaNode, out long pLeafNode, out long pMaxSumCost)
    {
        pLeafNode = pMaxSumCost = -1;

        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.CurrNode = pStaNode;
        WillPush.SumCost = 0;
        Stk.Push(WillPush);

        var VisitedSet = new HashSet<long>();
        VisitedSet.Add(pStaNode);

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

            if (pMaxSumCost < Popped.SumCost) {
                pMaxSumCost = Popped.SumCost;
                pLeafNode = Popped.CurrNode;
            }

            if (mEdgeInfoListDict.ContainsKey(Popped.CurrNode) == false)
                continue;

            foreach (EdgeInfoDef EachEdgeInfo in mEdgeInfoListDict[Popped.CurrNode]) {
                if (VisitedSet.Add(EachEdgeInfo.ToNode) == false) {
                    continue;
                }

                WillPush.CurrNode = EachEdgeInfo.ToNode;
                WillPush.SumCost = Popped.SumCost + EachEdgeInfo.Cost;
                Stk.Push(WillPush);
            }
        }
    }
}


解説

木なので、
(全ての辺のコスト * 2)の総和 - 木の直径
が解となります。