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

第4回PAST J ワープ


問題へのリンク


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

    static long mN;
    static long mM;

    static long mXAB;
    static long mXAC;
    static long mXBC;

    static string mS;

    struct ABCInfoDef
    {
        internal long A;
        internal long B;
        internal long C;
    }
    static List<ABCInfoDef> mABCInfoList = new List<ABCInfoDef>();

    struct EdgeInfoDef
    {
        internal long ToNode;
        internal long Cost;
    }
    // 隣接リスト
    static Dictionary<long, List<EdgeInfoDef>> mEdgeListDict = 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]);
        mN = wkArr[0];
        mM = wkArr[1];

        SplitAct(InputList[1]);
        mXAB = wkArr[0];
        mXAC = wkArr[1];
        mXBC = wkArr[2];

        mS = InputList[2];

        foreach (string EachStr in InputList.Skip(3)) {
            SplitAct(EachStr);
            ABCInfoDef WillAdd;
            WillAdd.A = wkArr[0];
            WillAdd.B = wkArr[1];
            WillAdd.C = wkArr[2];
            mABCInfoList.Add(WillAdd);
        }

        // 隣接リストの辺を用意する
        foreach (ABCInfoDef EachABCInfo in mABCInfoList) {
            if (mEdgeListDict.ContainsKey(EachABCInfo.A) == false) {
                mEdgeListDict[EachABCInfo.A] = new List<EdgeInfoDef>();
            }
            EdgeInfoDef WillAdd1;
            WillAdd1.ToNode = EachABCInfo.B;
            WillAdd1.Cost = EachABCInfo.C;
            mEdgeListDict[EachABCInfo.A].Add(WillAdd1);

            if (mEdgeListDict.ContainsKey(EachABCInfo.B) == false) {
                mEdgeListDict[EachABCInfo.B] = new List<EdgeInfoDef>();
            }
            EdgeInfoDef WillAdd2;
            WillAdd2.ToNode = EachABCInfo.A;
            WillAdd2.Cost = EachABCInfo.C;
            mEdgeListDict[EachABCInfo.B].Add(WillAdd2);
        }

        // 数値ノードからノードA、ノードBとCから数値ノード
        // -101がノードA,-102がノードB,-103がノードC
        mEdgeListDict[-101] = new List<EdgeInfoDef>();
        mEdgeListDict[-101].Add(new EdgeInfoDef() { ToNode = -102, Cost = mXAB });
        mEdgeListDict[-101].Add(new EdgeInfoDef() { ToNode = -103, Cost = mXAC });
        mEdgeListDict[-102] = new List<EdgeInfoDef>();
        mEdgeListDict[-103] = new List<EdgeInfoDef>();

        // 数値ノードからノードB、ノードAとCから数値ノード
        // -201がノードA,-202がノードB,-203がノードC
        mEdgeListDict[-201] = new List<EdgeInfoDef>();
        mEdgeListDict[-202] = new List<EdgeInfoDef>();
        mEdgeListDict[-202].Add(new EdgeInfoDef() { ToNode = -201, Cost = mXAB });
        mEdgeListDict[-202].Add(new EdgeInfoDef() { ToNode = -203, Cost = mXBC });
        mEdgeListDict[-203] = new List<EdgeInfoDef>();

        // 数値ノードからノードC、ノードAとBから数値ノード
        // -301がノードA,-302がノードB,-303がノードC
        mEdgeListDict[-301] = new List<EdgeInfoDef>();
        mEdgeListDict[-302] = new List<EdgeInfoDef>();
        mEdgeListDict[-303] = new List<EdgeInfoDef>();
        mEdgeListDict[-303].Add(new EdgeInfoDef() { ToNode = -301, Cost = mXAC });
        mEdgeListDict[-303].Add(new EdgeInfoDef() { ToNode = -302, Cost = mXBC });

        // ワープの辺を貼る
        for (int I = 0; I <= mS.Length - 1; I++) {
            int CurrNode = I + 1;
            if (mEdgeListDict.ContainsKey(CurrNode) == false) {
                mEdgeListDict[CurrNode] = new List<EdgeInfoDef>();
            }
            if (mS[I] == 'A') {
                mEdgeListDict[CurrNode].Add(new EdgeInfoDef() { ToNode = -101, Cost = 0 });
                mEdgeListDict[-201].Add(new EdgeInfoDef() { ToNode = CurrNode, Cost = 0 });
                mEdgeListDict[-301].Add(new EdgeInfoDef() { ToNode = CurrNode, Cost = 0 });
            }
            if (mS[I] == 'B') {
                mEdgeListDict[CurrNode].Add(new EdgeInfoDef() { ToNode = -202, Cost = 0 });
                mEdgeListDict[-102].Add(new EdgeInfoDef() { ToNode = CurrNode, Cost = 0 });
                mEdgeListDict[-302].Add(new EdgeInfoDef() { ToNode = CurrNode, Cost = 0 });
            }
            if (mS[I] == 'C') {
                mEdgeListDict[CurrNode].Add(new EdgeInfoDef() { ToNode = -303, Cost = 0 });
                mEdgeListDict[-103].Add(new EdgeInfoDef() { ToNode = CurrNode, Cost = 0 });
                mEdgeListDict[-203].Add(new EdgeInfoDef() { ToNode = CurrNode, Cost = 0 });
            }
        }

        Dijkstra();
    }

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

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

        //Enqueue処理
        Action<long> EnqueueAct = pFromNode =>
        {
            if (mEdgeListDict.ContainsKey(pFromNode) == false) {
                return;
            }
            foreach (EdgeInfoDef EachEdge in mEdgeListDict[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);
            EnqueueAct(Dequeued.Node);
        }

        //foreach (var EachPair in KakuteiNodeDict.OrderBy(X => X.Key)) {
        //    Console.WriteLine("{0} {1}", EachPair.Key, EachPair.Value);
        //}
        Console.WriteLine(KakuteiNodeDict[mN]);
    }
}

#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];
        long 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


解説

ワープ用のノードを共通で持つようにして、
グラフのノード数を減らしておいて、
ダイクストラ法を使ってます。