AtCoderのPAST    次のPASTの問題へ    前のPASTの問題へ

第9回PAST I 直通エレベーター


問題へのリンク


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

    static long mM;

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

        var AppearNodeSet = new HashSet<long>();
        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);
            long FromNode = wkArr[0];
            long ToNode = wkArr[1];
            long Cost = wkArr[2];
            if (mEdgeInfoListDict.ContainsKey(FromNode) == false) {
                mEdgeInfoListDict[FromNode] = new List<EdgeInfoDef>();
            }
            if (mEdgeInfoListDict.ContainsKey(ToNode) == false) {
                mEdgeInfoListDict[ToNode] = new List<EdgeInfoDef>();
            }
            EdgeInfoDef WillAdd1;
            WillAdd1.ToNode = wkArr[1];
            WillAdd1.Cost = Cost;
            mEdgeInfoListDict[FromNode].Add(WillAdd1);

            EdgeInfoDef WillAdd2;
            WillAdd2.ToNode = FromNode;
            WillAdd2.Cost = Cost;
            mEdgeInfoListDict[ToNode].Add(WillAdd2);

            AppearNodeSet.Add(FromNode);
            AppearNodeSet.Add(ToNode);
        }

        // ノードの昇順に階段の辺を作成
        AppearNodeSet.Add(1);
        AppearNodeSet.Add(mM);
        long[] AppearNodeArr = AppearNodeSet.OrderBy(pX => pX).ToArray();
        for (int I = 0; I <= AppearNodeArr.GetUpperBound(0) - 1; I++) {
            long FromNode = AppearNodeArr[I];
            long ToNode = AppearNodeArr[I + 1];
            long Cost = ToNode - FromNode;
            if (mEdgeInfoListDict.ContainsKey(FromNode) == false) {
                mEdgeInfoListDict[FromNode] = new List<EdgeInfoDef>();
            }
            if (mEdgeInfoListDict.ContainsKey(ToNode) == false) {
                mEdgeInfoListDict[ToNode] = new List<EdgeInfoDef>();
            }
            EdgeInfoDef WillAdd1;
            WillAdd1.ToNode = ToNode;
            WillAdd1.Cost = Cost;
            mEdgeInfoListDict[FromNode].Add(WillAdd1);

            EdgeInfoDef WillAdd2;
            WillAdd2.ToNode = FromNode;
            WillAdd2.Cost = Cost;
            mEdgeInfoListDict[ToNode].Add(WillAdd2);
        }

        // ダイクストラ法
        Dijkstra();
    }

    // ダイクストラ法で、各ノードまでの最短距離を求める
    static void Dijkstra()
    {
        var InsPQueue = new PQueue();

        // 距離合計[確定ノード]なDict
        var KakuteiNodeDict = new Dictionary<long, long>();
        KakuteiNodeDict.Add(1, 0);

        // Enqueue処理
        Action<long> EnqueueAct = pFromNode =>
        {
            if (mEdgeInfoListDict.ContainsKey(pFromNode) == false) {
                return;
            }
            foreach (EdgeInfoDef EachEdge in mEdgeInfoListDict[pFromNode]) {
                // 確定ノードならContinue
                if (KakuteiNodeDict.ContainsKey(EachEdge.ToNode)) continue;

                long wkSumCost = KakuteiNodeDict[pFromNode] + EachEdge.Cost;

                PQueue.PQueueJyoutaiDef WillEnqueue;
                WillEnqueue.Node = EachEdge.ToNode;
                WillEnqueue.SumCost = wkSumCost;
                InsPQueue.Enqueue(WillEnqueue);
            }
        };
        EnqueueAct(1);

        while (InsPQueue.IsEmpty() == false) {
            PQueue.PQueueJyoutaiDef Dequeued = InsPQueue.Dequeue();

            // 確定ノードならContinue
            if (KakuteiNodeDict.ContainsKey(Dequeued.Node)) continue;

            KakuteiNodeDict.Add(Dequeued.Node, Dequeued.SumCost);

            // ゴールへの最短距離が確定したら、枝切り
            if (KakuteiNodeDict.ContainsKey(mM)) break;

            EnqueueAct(Dequeued.Node);
        }

        Console.WriteLine(KakuteiNodeDict[mM]);
    }
}

#region PQueue
// 優先度付きキュー
internal class PQueue
{
    internal struct PQueueJyoutaiDef
    {
        internal long Node;
        internal long SumCost;
    }

    private Dictionary<long, PQueueJyoutaiDef> mHeapDict = new Dictionary<long, PQueueJyoutaiDef>();

    internal bool IsEmpty()
    {
        return mHeapDict.Count == 0;
    }

    // エンキュー処理
    internal void Enqueue(PQueueJyoutaiDef pAddJyoutai)
    {
        long CurrNode = 1 + mHeapDict.Count;
        mHeapDict[CurrNode] = pAddJyoutai;

        while (1 < CurrNode && mHeapDict[CurrNode / 2].SumCost > mHeapDict[CurrNode].SumCost) {
            PQueueJyoutaiDef Swap = mHeapDict[CurrNode];
            mHeapDict[CurrNode] = mHeapDict[CurrNode / 2];
            mHeapDict[CurrNode / 2] = Swap;

            CurrNode /= 2;
        }
    }

    // デキュー処理
    internal PQueueJyoutaiDef Dequeue()
    {
        PQueueJyoutaiDef TopNode = mHeapDict[1];
        int LastNode = mHeapDict.Count;
        mHeapDict[1] = mHeapDict[LastNode];
        mHeapDict.Remove(LastNode);

        MinHeapify(1);
        return TopNode;
    }

    // 根ノードを指定し、根から葉へヒープ構築
    private void MinHeapify(long pRootNode)
    {
        if (mHeapDict.Count <= 1) {
            return;
        }

        long Left = pRootNode * 2;
        long Right = pRootNode * 2 + 1;

        // 左の子、自分、右の子で値が最小のノードを選ぶ
        long Smallest = mHeapDict[pRootNode].SumCost;
        long SmallestNode = pRootNode;

        if (mHeapDict.ContainsKey(Left) && mHeapDict[Left].SumCost < Smallest) {
            Smallest = mHeapDict[Left].SumCost;
            SmallestNode = Left;
        }
        if (mHeapDict.ContainsKey(Right) && mHeapDict[Right].SumCost < Smallest) {
            Smallest = mHeapDict[Right].SumCost;
            SmallestNode = Right;
        }

        // 子ノードのほうが大きい場合
        if (SmallestNode != pRootNode) {
            PQueueJyoutaiDef Swap = mHeapDict[SmallestNode];
            mHeapDict[SmallestNode] = mHeapDict[pRootNode];
            mHeapDict[pRootNode] = Swap;

            // 再帰的に呼び出し
            MinHeapify(SmallestNode);
        }
    }
}
#endregion


解説

階段の無向辺を追加してから、ダイクストラ法で解いてます。