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

ABC192-E Train


問題へのリンク


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

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

    static int mX;
    static int mY;

    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]);
        mX = wkArr[2];
        mY = wkArr[3];

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

            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;
            WillAdd1.K = K;
            mEdgeInfoListDict[FromNode].Add(WillAdd1);

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

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

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

        //Enqueue処理
        Action<int> 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];

                // 待ち時間を足す
                long ModVal = wkSumCost % EachEdge.K;
                if (ModVal != 0) {
                    wkSumCost += EachEdge.K - ModVal;
                }

                wkSumCost += EachEdge.Cost;

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

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

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

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

        if (KakuteiNodeDict.ContainsKey(mY)) {
            Console.WriteLine(KakuteiNodeDict[mY]);
        }
        else {
            Console.WriteLine(-1);
        }
    }
}

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

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

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

    // エンキュー処理
    internal void Enqueue(PQueueJyoutaiDef pAddJyoutai)
    {
        int 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(int pRootNode)
    {
        if (mHeapDict.Count <= 1) {
            return;
        }

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

        // 左の子、自分、右の子で値が最小のノードを選ぶ
        long Smallest = mHeapDict[pRootNode].SumCost;
        int 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


解説

ダイクストラ法をアレンジして、
エンキューする際に、待ち時間をコストに加算してます。