AtCoderの企業コンテスト    次の企業コンテストの問題へ    前の企業コンテストの問題へ

CodeQUEEN 2024 決勝 D Attend Many Events


問題へのリンク


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 3 3");
            WillReturn.Add("1 2 1");
            WillReturn.Add("1 3 3");
            WillReturn.Add("2 3 7");
            WillReturn.Add("1 8");
            WillReturn.Add("2 2");
            WillReturn.Add("3 7");
            //2
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("1 0 2");
            WillReturn.Add("1 1");
            WillReturn.Add("1 100");
            //2
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("5 8 10");
            WillReturn.Add("1 2 149622151");
            WillReturn.Add("1 4 177783960");
            WillReturn.Add("4 5 118947237");
            WillReturn.Add("1 3 33222944");
            WillReturn.Add("1 5 295060863");
            WillReturn.Add("3 5 110881471");
            WillReturn.Add("2 3 34104208");
            WillReturn.Add("3 4 273071547");
            WillReturn.Add("2 650287940");
            WillReturn.Add("4 839296263");
            WillReturn.Add("3 462224593");
            WillReturn.Add("1 492601449");
            WillReturn.Add("4 384836991");
            WillReturn.Add("1 191890310");
            WillReturn.Add("5 576823355");
            WillReturn.Add("3 782177068");
            WillReturn.Add("3 404011431");
            WillReturn.Add("1 818008580");
            //6
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

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

    struct EventInfo
    {
        internal long Node;
        internal long Time;
    }
    static List<EventInfo> mEventList = new List<EventInfo>();

    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 M = wkArr[1];

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

            Action<long, long, long> AddEdgeAct = (pFromNode, pToNode, pCost) =>
            {
                if (mEdgeInfoListDict.ContainsKey(pFromNode) == false) {
                    mEdgeInfoListDict[pFromNode] = new List<EdgeInfoDef>();
                }
                EdgeInfoDef WillAdd;
                WillAdd.ToNode = pToNode;
                WillAdd.Cost = pCost;
                mEdgeInfoListDict[pFromNode].Add(WillAdd);
            };
            AddEdgeAct(FromNode, ToNode, Cost);
            AddEdgeAct(ToNode, FromNode, Cost);
        }

        // 番兵なイベント
        EventInfo WillAdd1;
        WillAdd1.Node = 1;
        WillAdd1.Time = 0;
        mEventList.Add(WillAdd1);

        foreach (string EachStr in InputList.Skip(1 + (int)M)) {
            SplitAct(EachStr);
            EventInfo WillAdd2;
            WillAdd2.Node = wkArr[0];
            WillAdd2.Time = wkArr[1];
            mEventList.Add(WillAdd2);
        }
        mEventList = mEventList.OrderBy(pX => pX.Time).ToList();

        // 参加イベント数[現在Ind]なDP表
        var PrevDP = new Dictionary<int, long>();
        PrevDP[0] = 0;

        long Answer = 0;

        while (PrevDP.Count > 0) {
            var CurrDP = new Dictionary<int, long>();
            foreach (var EachPair in PrevDP) {
                long CurrNode = mEventList[EachPair.Key].Node;
                long CurrTime = mEventList[EachPair.Key].Time;

                Dictionary<long, long> Result = Dijkstra(CurrNode);

                for (int I = EachPair.Key + 1; I <= mEventList.Count - 1; I++) {
                    if (CurrDP.ContainsKey(I)) continue;

                    if (Result.ContainsKey(mEventList[I].Node) == false) continue;

                    long TimeDiff = mEventList[I].Time - CurrTime;
                    if (Result[mEventList[I].Node] > TimeDiff) continue;

                    long NewVal = EachPair.Value + 1;
                    CurrDP[I] = NewVal;
                    Answer = Math.Max(Answer, NewVal);
                }
            }
            PrevDP = CurrDP;
        }
        Console.WriteLine(Answer);
    }

    // ダイクストラ法で、各ノードまでの最短距離を求める
    static Dictionary<long, Dictionary<long, long>> mMemo = new Dictionary<long, Dictionary<long, long>>();
    static Dictionary<long, long> Dijkstra(long pStaNode)
    {
        if (mMemo.ContainsKey(pStaNode)) return mMemo[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;

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

        return mMemo[pStaNode] = KakuteiNodeDict;
    }
}

#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


解説

参加イベント数[現在Ind]なDP表を更新してます。