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

ABC325-E Our clients, please wait a moment


問題へのリンク


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 8 5 13");
            WillReturn.Add("0 6 2 15");
            WillReturn.Add("6 0 3 5");
            WillReturn.Add("2 3 0 13");
            WillReturn.Add("15 5 13 0");
            //78
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("3 1 1000000 1000000");
            WillReturn.Add("0 10 1");
            WillReturn.Add("10 0 10");
            WillReturn.Add("1 10 0");
            //1
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("5 954257 954213 814214");
            WillReturn.Add("0 84251 214529 10017 373342");
            WillReturn.Add("84251 0 91926 32336 164457");
            WillReturn.Add("214529 91926 0 108914 57762");
            WillReturn.Add("10017 32336 108914 0 234705");
            WillReturn.Add("373342 164457 57762 234705 0");
            //168604826785
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static long mN;
    static long mA;
    static long mB;
    static long mC;

    static int[,] mBanArr;
    static int UB;

    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]);
        mN = wkArr[0];
        mA = wkArr[1];
        mB = wkArr[2];
        mC = wkArr[3];

        mBanArr = CreateBanArr(InputList.Skip(1));
        UB = mBanArr.GetUpperBound(0);

        Action<long, long, long> AddAct = (pFrom, pTo, pCost) =>
        {
            if (mEdgeInfoListDict.ContainsKey(pFrom) == false) {
                mEdgeInfoListDict[pFrom] = new List<EdgeInfoDef>();
            }
            EdgeInfoDef WillAdd;
            WillAdd.ToNode = pTo;
            WillAdd.Cost = pCost;
            mEdgeInfoListDict[pFrom].Add(WillAdd);
        };

        // 車の移動
        mEdgeInfoListDict.Clear();
        for (int X = 0; X <= UB; X++) {
            for (int Y = 0; Y <= UB; Y++) {
                if (X == Y) continue;

                // 車の移動の無向辺を追加
                long Cost = mBanArr[X, Y] * mA;
                AddAct(X, Y, Cost);
                AddAct(Y, X, Cost);
            }
        }

        Dictionary<long, long> KakuteiNodeDictCarFromSta = Dijkstra(0);
        Dictionary<long, long> KakuteiNodeDictCarFromGoal = Dijkstra(mN - 1);

        // 電車の移動
        mEdgeInfoListDict.Clear();
        for (int X = 0; X <= UB; X++) {
            for (int Y = 0; Y <= UB; Y++) {
                if (X == Y) continue;

                // 電車の移動の無向辺を追加
                long Cost = mBanArr[X, Y] * mB + mC;
                AddAct(X, Y, Cost);
                AddAct(Y, X, Cost);
            }
        }
        Dictionary<long, long> KakuteiNodeDictTrainFromGoal = Dijkstra(mN - 1);

        // 車から電車への乗り換えを全て試す
        long Answer = long.MaxValue;
        for (long I = 0; I <= mN - 1; I++) {
            var AnswerKouho = new List<long>();
            AnswerKouho.Add(KakuteiNodeDictCarFromSta[I] + KakuteiNodeDictCarFromGoal[I]);
            AnswerKouho.Add(KakuteiNodeDictCarFromSta[I] + KakuteiNodeDictTrainFromGoal[I]);
            Answer = Math.Min(Answer, AnswerKouho.Min());
        }
        Console.WriteLine(Answer);
    }

    // ダイクストラ法で、各ノードまでの最短距離を求める
    static Dictionary<long, long> Dijkstra(long pStaNode)
    {
        var InsPQueue = new PQueue_Arr();

        // 距離合計[確定ノード]なDict
        var KakuteiNodeDict = new Dictionary<long, long>();
        KakuteiNodeDict.Add(pStaNode, 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_Arr.PQueueJyoutaiDef WillEnqueue;
                WillEnqueue.Node = EachEdge.ToNode;
                WillEnqueue.SumCost = wkSumCost;
                InsPQueue.Enqueue(WillEnqueue);
            }
        };
        EnqueueAct(pStaNode);

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

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

            // 枝切り
            // if (KakuteiNodeDict.ContainsKey(pGoalNode)) break;

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

        return KakuteiNodeDict;
    }

    ////////////////////////////////////////////////////////////////
    // IEnumerable<string>をintの2次元配列に設定する
    ////////////////////////////////////////////////////////////////
    static int[,] CreateBanArr(IEnumerable<string> pStrEnum)
    {
        var StrList = pStrEnum.ToList();
        if (StrList.Count == 0) {
            return new int[0, 0];
        }

        int[] IntArr = { };
        Action<string> SplitAct = pStr =>
            IntArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();

        SplitAct(StrList[0]);

        int UB_X = IntArr.GetUpperBound(0);
        int UB_Y = StrList.Count - 1;

        int[,] WillReturn = new int[UB_X + 1, UB_Y + 1];

        for (int Y = 0; Y <= UB_Y; Y++) {
            SplitAct(StrList[Y]);
            for (int X = 0; X <= UB_X; X++) {
                WillReturn[X, Y] = IntArr[X];
            }
        }
        return WillReturn;
    }
}

#region PQueue_Arr
// 内部で配列使用の優先度付きキュー
internal class PQueue_Arr
{
    internal struct PQueueJyoutaiDef
    {
        internal long Node;
        internal long SumCost;
    }

    private PQueueJyoutaiDef[] mHeapArr;
    private long mHeapArrCnt = 0;

    // コンストラクタ
    internal PQueue_Arr()
    {
        mHeapArr = new PQueueJyoutaiDef[65535];
    }
    internal bool IsEmpty()
    {
        return mHeapArrCnt == 0;
    }

    // エンキュー処理
    internal void Enqueue(PQueueJyoutaiDef pAddJyoutai)
    {
        long CurrNode = 1 + mHeapArrCnt;
        if (mHeapArr.GetUpperBound(0) < CurrNode) {
            ExtendArr();
        }
        mHeapArr[CurrNode] = pAddJyoutai;
        mHeapArrCnt++;

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

            CurrNode /= 2;
        }
    }

    // 配列のExtend
    private void ExtendArr()
    {
        PQueueJyoutaiDef[] NewHeapArr = new PQueueJyoutaiDef[mHeapArrCnt * 2];
        mHeapArr.CopyTo(NewHeapArr, 0);
        mHeapArr = NewHeapArr;
    }

    // デキュー処理
    internal PQueueJyoutaiDef Dequeue()
    {
        PQueueJyoutaiDef TopNode = mHeapArr[1];
        long LastNode = mHeapArrCnt;
        mHeapArr[1] = mHeapArr[LastNode];
        mHeapArrCnt--;

        MinHeapify(1);
        return TopNode;
    }

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

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

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

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

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

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


解説

ダイクストラ法の計算量は
プライオリティキューを使えば、O(E * logV)なので
辺の数がなるべく少ない状態で
ダイクストラ法を行うことを考えます。

Staノードから車のみの移動での各ノードまでの距離
Endノードから車のみの移動での各ノードまでの距離
Endノードから電車のみの移動での各ノードまでの距離

を3回のダイクストラ法で求めて、車から電車に変更するノードを全て試せば
解を求めることができます。