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

ABC035-D トレジャーハント


問題へのリンク


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("2 2 5");
            WillReturn.Add("1 3");
            WillReturn.Add("1 2 2");
            WillReturn.Add("2 1 1");
            //6
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("2 2 3");
            WillReturn.Add("1 3");
            WillReturn.Add("1 2 2");
            WillReturn.Add("2 1 1");
            //3
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("8 15 120");
            WillReturn.Add("1 2 6 16 1 3 11 9");
            WillReturn.Add("1 8 1");
            WillReturn.Add("7 3 14");
            WillReturn.Add("8 2 13");
            WillReturn.Add("3 5 4");
            WillReturn.Add("5 7 5");
            WillReturn.Add("6 4 1");
            WillReturn.Add("6 8 17");
            WillReturn.Add("7 8 5");
            WillReturn.Add("1 4 2");
            WillReturn.Add("4 7 1");
            WillReturn.Add("6 1 3");
            WillReturn.Add("3 1 10");
            WillReturn.Add("2 6 5");
            WillReturn.Add("2 4 12");
            WillReturn.Add("5 1 30");
            //1488
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct EdgeInfoDef
    {
        internal int ToNode;
        internal int Cost;
    }

    // 隣接リスト (正辺)
    static Dictionary<int, List<EdgeInfoDef>> mSeiEdgeListDict = new Dictionary<int, List<EdgeInfoDef>>();

    // 隣接リスト (逆辺)
    static Dictionary<int, List<EdgeInfoDef>> mRevEdgeListDict = new Dictionary<int, 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 N = wkArr[0];
        long T = wkArr[2];

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

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

            if (mSeiEdgeListDict.ContainsKey(FromNode) == false) {
                mSeiEdgeListDict[FromNode] = new List<EdgeInfoDef>();
            }
            EdgeInfoDef WillAddSei;
            WillAddSei.ToNode = ToNode;
            WillAddSei.Cost = Cost;
            mSeiEdgeListDict[FromNode].Add(WillAddSei);

            if (mRevEdgeListDict.ContainsKey(ToNode) == false) {
                mRevEdgeListDict[ToNode] = new List<EdgeInfoDef>();
            }
            EdgeInfoDef WillAddRev;
            WillAddRev.ToNode = FromNode;
            WillAddRev.Cost = Cost;
            mRevEdgeListDict[ToNode].Add(WillAddRev);
        }
        var SeiKakuteiNodeDict = Dijkstra(mSeiEdgeListDict);
        var RevKakuteiNodeDict = Dijkstra(mRevEdgeListDict);

        // 滞在するノードを全探索して、解を求める
        long Answer = 0;
        var NodeEnum = SeiKakuteiNodeDict.Keys.Intersect(RevKakuteiNodeDict.Keys);
        foreach (var EachNode in NodeEnum) {
            long RestMinute = T - SeiKakuteiNodeDict[EachNode] - RevKakuteiNodeDict[EachNode];
            long AnswerKouho = RestMinute * CArr[EachNode - 1];
            Answer = Math.Max(Answer, AnswerKouho);
        }
        Console.WriteLine(Answer);
    }

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

        // 距離合計[確定ノード]なDict
        var KakuteiNodeDict = new Dictionary<int, int>();
        KakuteiNodeDict.Add(1, 0);

        //Enqueue処理
        Action<int> EnqueueAct = pFromNode =>
        {
            if (pEdgeArrList.ContainsKey(pFromNode)) {
                foreach (EdgeInfoDef EachEdge in pEdgeArrList[pFromNode]) {
                    // 確定ノードならContinue
                    if (KakuteiNodeDict.ContainsKey(EachEdge.ToNode)) continue;

                    int wkSumCost = KakuteiNodeDict[pFromNode] + EachEdge.Cost;

                    PQueue.PQueueJyoutaiDef WillEnqueue;
                    WillEnqueue.Node = EachEdge.ToNode;
                    WillEnqueue.SumCost = wkSumCost;
                    InsPQueue.Enqueue(WillEnqueue);
                }
            }
        };
        EnqueueAct(1);

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

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

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

#region PQueue
// 優先度付きキュー
internal class PQueue
{
    internal struct PQueueJyoutaiDef
    {
        internal int Node;
        internal int SumCost;
    }

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

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

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

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

            CurrNode /= 2;
        }
    }

    // デキュー処理
    internal PQueueJyoutaiDef Dequeue()
    {
        PQueueJyoutaiDef TopNode = mHeapDict[1];
        int LastNode = mHeapDict.Count;
        mHeapDict[1] = mHeapDict[LastNode];
        mHeapDict.Remove(LastNode);

        MinHeapify(1);
        return TopNode;
    }

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

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

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

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

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

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


解説

頂点1から、全てのノードまでの距離をダイクストラで求めます。

次に、辺の向きを全て逆にして
頂点1までの、全てのノードからの距離をダイクストラで求めます。

滞在ノードを全探索すれば、解が求まります。