AtCoderの有志コンテスト    次の有志コンテストの問題へ    前の有志コンテストの問題へ

GigaCode 2019 E 車の乗り継ぎ


問題へのリンク


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 10");
            WillReturn.Add("1 5");
            WillReturn.Add("3 5 8");
            WillReturn.Add("6 10 5");
            WillReturn.Add("7 2 7");
            //4.000000000000000000000
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("3 10");
            WillReturn.Add("1 5");
            WillReturn.Add("3 5 8");
            WillReturn.Add("6 1 5");
            WillReturn.Add("7 2 7");
            //4.400000000000000355271
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("2 10");
            WillReturn.Add("1 4");
            WillReturn.Add("3 1 2");
            WillReturn.Add("6 1 10");
            //impossible
        }
        else if (InputPattern == "Input4") {
            WillReturn.Add("0 1");
            WillReturn.Add("99991 1");
            //0.000010000900081007291
        }
        else if (InputPattern == "Input5") {
            WillReturn.Add("1 100");
            WillReturn.Add("5 60");
            WillReturn.Add("50 7 90");
            //17.142857142857142349612
        }
        else if (InputPattern == "Input6") {
            WillReturn.Add("4 1000");
            WillReturn.Add("37 426");
            WillReturn.Add("725 16 612");
            WillReturn.Add("237 19 458");
            WillReturn.Add("516 13 509");
            WillReturn.Add("408 17 400");
            //46.861585850556437549130
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct CarInfoDef
    {
        internal long StaPos;
        internal long EndPos;
        internal long Speed;
    }
    static List<CarInfoDef> mCarInfoList = new List<CarInfoDef>();

    struct EdgeInfoDef
    {
        internal long ToNode;
        internal decimal 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]);
        long Goal = wkArr[1];

        SplitAct(InputList[1]);
        long V = wkArr[0];
        long D = wkArr[1];

        CarInfoDef WillAdd1;
        WillAdd1.StaPos = 0;
        WillAdd1.EndPos = D;
        WillAdd1.Speed = V;
        mCarInfoList.Add(WillAdd1);

        foreach (string EachStr in InputList.Skip(2)) {
            SplitAct(EachStr);
            CarInfoDef WillAdd2;
            WillAdd2.StaPos = wkArr[0];
            WillAdd2.Speed = wkArr[1];
            WillAdd2.EndPos = wkArr[0] + wkArr[2];
            mCarInfoList.Add(WillAdd2);
        }
        mCarInfoList = mCarInfoList.OrderBy(pX => pX.StaPos).ToList();

        for (int I = 0; I <= mCarInfoList.Count - 1; I++) {
            CarInfoDef FromNode = mCarInfoList[I];
            mEdgeInfoListDict[FromNode.StaPos] = new List<EdgeInfoDef>();
            for (int J = I + 1; J <= mCarInfoList.Count - 1; J++) {
                CarInfoDef ToNode = mCarInfoList[J];
                if (FromNode.StaPos <= ToNode.StaPos && ToNode.StaPos <= FromNode.EndPos) {
                    EdgeInfoDef WillAdd;
                    WillAdd.ToNode = ToNode.StaPos;
                    WillAdd.Cost = (ToNode.StaPos - FromNode.StaPos);
                    WillAdd.Cost /= FromNode.Speed;
                    mEdgeInfoListDict[FromNode.StaPos].Add(WillAdd);
                }
            }
        }

        var AnswerKouhoList = new List<decimal>();

        Dictionary<long, decimal> ResultDict = Dijkstra(0);
        foreach (var EachPair in ResultDict) {
            //Console.WriteLine("ResultDict[{0}]={1}", EachPair.Key, EachPair.Value);

            foreach (CarInfoDef EachCarInfo in mCarInfoList) {
                if (EachCarInfo.StaPos == EachPair.Key) {
                    if (EachCarInfo.EndPos >= Goal) {
                        decimal AddCost = (Goal - EachCarInfo.StaPos);
                        AddCost /= EachCarInfo.Speed;
                        decimal Kouho = EachPair.Value + AddCost;
                        AnswerKouhoList.Add(Kouho);
                    }
                }
            }
        }

        if (AnswerKouhoList.Count > 0) {
            Console.WriteLine(AnswerKouhoList.Min());
        }
        else {
            Console.WriteLine("impossible");
        }
    }

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

        // 距離合計[確定ノード]なDict
        var KakuteiNodeDict = new Dictionary<long, decimal>();
        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;

                decimal 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;

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

        return KakuteiNodeDict;
    }
}

#region PQueue_Arr
// 内部で配列使用の優先度付きキュー
internal class PQueue_Arr
{
    internal struct PQueueJyoutaiDef
    {
        internal long Node;
        internal decimal 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;

        // 左の子、自分、右の子で値が最小のノードを選ぶ
        decimal 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(枝数 + ノード数)なので、
間に合います。