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

第6回PAST G 一日一歩


問題へのリンク


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

    static long mM;

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

    static long[] mXArr;

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

        foreach (string EachStr in InputList.Skip(1).Take((int)mM)) {
            SplitAct(EachStr);
            long A = wkArr[0];
            long B = wkArr[1];
            long C = wkArr[2];

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

            EdgeInfoDef WillAdd2;
            WillAdd2.ToNode = A;
            WillAdd2.Cost = C;
            mEdgeInfoListDict[B].Add(WillAdd2);
        }

        mXArr = InputList.Last().Split(' ').Select(pX => long.Parse(pX)).ToArray();

        Solve();
    }

    static void Solve()
    {
        var InsPQueue = new PQueue();
        PQueue.PQueueJyoutaiDef WillEnqueue;

        // 訪問済ノード
        var VisitedSet = new HashSet<long>();
        VisitedSet.Add(1);

        // 指定した頂点からの辺をプライオリティーキューに入れる
        Action<long> EnqueueAct = pNode =>
        {
            if (mEdgeInfoListDict.ContainsKey(pNode)) {
                foreach (EdgeInfoDef EachEdgeInfo in mEdgeInfoListDict[pNode]) {
                    if (VisitedSet.Contains(EachEdgeInfo.ToNode) == false) {
                        WillEnqueue.ToNode = EachEdgeInfo.ToNode;
                        WillEnqueue.Cost = EachEdgeInfo.Cost;
                        InsPQueue.Enqueue(WillEnqueue);
                    }
                }
            }
        };

        EnqueueAct(1);

        var sb = new System.Text.StringBuilder();
        foreach (long EachX in mXArr) {
            var CanVisitNodeList = new List<long>();
            while (InsPQueue.IsEmpty() == false) {
                if (EachX < InsPQueue.Peek().Cost) {
                    break;
                }
                PQueue.PQueueJyoutaiDef Dequeued = InsPQueue.Dequeue();
                if (VisitedSet.Add(Dequeued.ToNode)) {
                    CanVisitNodeList.Add(Dequeued.ToNode);
                }
            }
            CanVisitNodeList.ForEach(pX =>
            {
                EnqueueAct(pX);
                VisitedSet.Add(pX);
            });

            sb.AppendLine(VisitedSet.Count.ToString());
        }
        Console.Write(sb.ToString());
    }
}

#region PQueue
// 優先度付きキュー
internal class PQueue
{
    internal struct PQueueJyoutaiDef
    {
        internal long ToNode;
        internal long Cost;
    }

    private Dictionary<long, PQueueJyoutaiDef> mHeapDict = new Dictionary<long, PQueueJyoutaiDef>();

    internal bool IsEmpty()
    {
        return mHeapDict.Count == 0;
    }

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

    // エンキュー処理
    internal void Enqueue(PQueueJyoutaiDef pAddJyoutai)
    {
        long CurrNode = 1 + mHeapDict.Count;
        mHeapDict[CurrNode] = pAddJyoutai;

        while (1 < CurrNode && mHeapDict[CurrNode / 2].Cost > mHeapDict[CurrNode].Cost) {
            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].Cost;
        long SmallestNode = pRootNode;

        if (mHeapDict.ContainsKey(Left) && mHeapDict[Left].Cost < Smallest) {
            Smallest = mHeapDict[Left].Cost;
            SmallestNode = Left;
        }
        if (mHeapDict.ContainsKey(Right) && mHeapDict[Right].Cost < Smallest) {
            Smallest = mHeapDict[Right].Cost;
            SmallestNode = Right;
        }

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

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


解説

プリム法をアレンジして解いてます。