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

第7回PAST K 急ぎ旅


問題へのリンク


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

    static long mN; // 頂点の数

    struct EdgeInfoDef
    {
        internal long ToNode;
        internal long Time;
    }
    static Dictionary<long, List<EdgeInfoDef>> mEdgeInfoListDict = new Dictionary<long, List<EdgeInfoDef>>();

    static int[] mAArr;

    static void Main()
    {
        List<string> InputList = GetInputList();

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

        SplitAct(InputList[0]);
        mN = wkArr[0];

        mAArr = InputList[1].Split(' ').Select(pX => int.Parse(pX)).ToArray();

        foreach (string EachStr in InputList.Skip(2)) {
            SplitAct(EachStr);
            int FromNode = wkArr[0];
            int ToNode = wkArr[1];
            int Time = 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 = ToNode;
            WillAdd1.Time = Time;
            mEdgeInfoListDict[FromNode].Add(WillAdd1);

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

        Dijkstra();
    }

    struct CostInfoDef
    {
        internal long TimeSum;
        internal long KeikanSum;
    }

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

        // コスト情報[確定ノード]なDict
        var KakuteiNodeDict = new Dictionary<long, CostInfoDef>();
        KakuteiNodeDict.Add(1, new CostInfoDef() { TimeSum = 0, KeikanSum = mAArr[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 CurrTimeSum = KakuteiNodeDict[pFromNode].TimeSum;
                long CurrKeikanSum = KakuteiNodeDict[pFromNode].KeikanSum;

                PQueue_Arr.PQueueJyoutaiDef WillEnqueue;
                WillEnqueue.Node = EachEdge.ToNode;
                WillEnqueue.TimeSum = CurrTimeSum + EachEdge.Time;
                WillEnqueue.KeikanSum = CurrKeikanSum + mAArr[EachEdge.ToNode - 1];
                InsPQueue.Enqueue(WillEnqueue);
            }
        };
        EnqueueAct(1);

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

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

            KakuteiNodeDict.Add(Dequeued.Node,
                new CostInfoDef() { TimeSum = Dequeued.TimeSum, KeikanSum = Dequeued.KeikanSum });
            EnqueueAct(Dequeued.Node);

            if (Dequeued.Node == mN) {
                break;
            }
        }

        Console.WriteLine(KakuteiNodeDict[mN].KeikanSum);
    }
}

#region PQueue_Arr
// 優先度付きキュー
internal class PQueue_Arr
{
    internal struct PQueueJyoutaiDef : IComparable<PQueueJyoutaiDef>
    {
        internal long Node;
        internal long TimeSum;
        internal long KeikanSum;

        // OrderBy 時間合計 ASC , 景観合計 DESC
        public int CompareTo(PQueueJyoutaiDef pOtherIns)
        {
            if (TimeSum != pOtherIns.TimeSum) {
                return TimeSum.CompareTo(pOtherIns.TimeSum);
            }
            return pOtherIns.KeikanSum.CompareTo(KeikanSum);
        }
    }

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

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

    internal bool IsEmpty()
    {
        return mHeapArrCnt == 0;
    }

    internal long Count()
    {
        return mHeapArrCnt;
    }

    internal PQueueJyoutaiDef Peek()
    {
        return mHeapArr[1];
    }

    // エンキュー処理
    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].CompareTo(mHeapArr[CurrNode]) > 0) {
            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(int pRootNode)
    {
        if (mHeapArrCnt <= 1) {
            return;
        }

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

        // 左の子、自分、右の子で値が最小のノードを選ぶ
        int SmallestNode = pRootNode;

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

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

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


解説

プライオリティキューのソート条件を
OrderBy 時間合計 ASC , 景観合計 DESC
の複数ソート条件として、
ダイクストラ法でゴールまでのノードの
最小の時間合計での、景観合計の最大値を求めてます。