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

ABC373-D Hidden Weights


問題へのリンク


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 3");
            WillReturn.Add("1 2 2");
            WillReturn.Add("3 2 3");
            WillReturn.Add("1 3 -1");
            //3 5 2
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("4 2");
            WillReturn.Add("2 1 5");
            WillReturn.Add("3 4 -3");
            //5 0 6 3
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("5 7");
            WillReturn.Add("2 1 18169343");
            WillReturn.Add("3 1 307110901");
            WillReturn.Add("4 1 130955934");
            WillReturn.Add("2 3 -288941558");
            WillReturn.Add("2 5 96267410");
            WillReturn.Add("5 3 -385208968");
            WillReturn.Add("4 3 -176154967");
            //200401298 182231955 -106709603 69445364 278499365
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct EdgeInfoDef
    {
        internal long ToNode;
        internal long Cost;
    }
    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(pX => long.Parse(pX)).ToArray();

        SplitAct(InputList[0]);
        long NodeCnt = wkArr[0];

        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);
            long FromNode = wkArr[0];
            long ToNpde = wkArr[1];
            long AddVal = wkArr[2];

            Action<long, long, long> 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(FromNode, ToNpde, AddVal);
            AddEdgeAct(ToNpde, FromNode, -1 * AddVal);
        }

        // 書いた値[ノード]なDict
        var NumDict = new Dictionary<long, long>();

        for (long I = 1; I <= NodeCnt; I++) {
            if (NumDict.ContainsKey(I)) continue;
            NumDict[I] = 0;

            var Stk = new Stack<JyoutaiDef>();
            JyoutaiDef WillPush;
            WillPush.CurrNode = I;
            Stk.Push(WillPush);

            while (Stk.Count > 0) {
                JyoutaiDef Popped = Stk.Pop();
                long CurrNode = Popped.CurrNode;
                if (mEdgeInfoListDict.ContainsKey(CurrNode)) {
                    foreach (EdgeInfoDef EachEdgeInfo in mEdgeInfoListDict[CurrNode]) {
                        if (NumDict.ContainsKey(EachEdgeInfo.ToNode)) {
                            continue;
                        }
                        NumDict[EachEdgeInfo.ToNode] = NumDict[CurrNode] + EachEdgeInfo.Cost;
                        WillPush.CurrNode = EachEdgeInfo.ToNode;
                        Stk.Push(WillPush);
                    }
                }
            }
        }

        var AnswerList = new List<long>();
        foreach (var EachPair in NumDict.OrderBy(pX => pX.Key)) {
            AnswerList.Add(EachPair.Value);
        }
        Console.WriteLine(LongEnumJoin(" ", AnswerList));
    }

    struct JyoutaiDef
    {
        internal long CurrNode;
    }

    // セパレータとLong型の列挙を引数として、結合したstringを返す
    static string LongEnumJoin(string pSeparater, IEnumerable<long> pEnum)
    {
        string[] StrArr = Array.ConvertAll(pEnum.ToArray(), pX => pX.ToString());
        return string.Join(pSeparater, StrArr);
    }
}


解説

高低差が指定されてると考え、
逆辺も用意し、未確定ノードならDFSで確定させてます。