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

第5回PAST I 避難計画


問題へのリンク


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

    static int mN;
    static int[] mHArr;
    static int[] mCArr;

    // 隣接リスト
    static Dictionary<int, List<int>> mToNodeListDict = new Dictionary<int, List<int>>();

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

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

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

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

        foreach (string EachStr in InputList.Skip(3)) {
            SplitAct(EachStr);
            int A = wkArr[0];
            int B = wkArr[1];

            int HeightA = mHArr[A - 1];
            int HeightB = mHArr[B - 1];

            bool AtoB = false;
            bool BtoA = false;
            if (HeightA < HeightB) {
                AtoB = true; // 逆辺を貼るので、AtoB
            }
            if (HeightA == HeightB) {
                AtoB = true;
                BtoA = true;
            }
            if (HeightA > HeightB) {
                BtoA = true; // 逆辺を貼るので、BtoA
            }

            if (AtoB) {
                if (mToNodeListDict.ContainsKey(A) == false) {
                    mToNodeListDict[A] = new List<int>();
                }
                mToNodeListDict[A].Add(B);
            }
            if (BtoA) {
                if (mToNodeListDict.ContainsKey(B) == false) {
                    mToNodeListDict[B] = new List<int>();
                }
                mToNodeListDict[B].Add(A);
            }
        }
        Solve();
    }

    struct JyoutaiDef
    {
        internal int CurrNode;
        internal int Level;
    }

    static void Solve()
    {
        var Que = new Queue<JyoutaiDef>();
        JyoutaiDef WillEnqueue;

        // 最小レベル[ノード]なDict
        var MinLevelDict = new Dictionary<int, int>();

        foreach (int EachC in mCArr) {
            WillEnqueue.CurrNode = EachC;
            WillEnqueue.Level = 0;
            Que.Enqueue(WillEnqueue);
            MinLevelDict[EachC] = 0;
        }

        while (Que.Count > 0) {
            JyoutaiDef Dequeued = Que.Dequeue();

            if (mToNodeListDict.ContainsKey(Dequeued.CurrNode)) {
                foreach (int EachToNode in mToNodeListDict[Dequeued.CurrNode]) {
                    if (MinLevelDict.ContainsKey(EachToNode)) {
                        continue;
                    }
                    WillEnqueue.CurrNode = EachToNode;
                    WillEnqueue.Level = Dequeued.Level + 1;
                    Que.Enqueue(WillEnqueue);
                    MinLevelDict[EachToNode] = WillEnqueue.Level;
                }
            }
        }

        var sb = new System.Text.StringBuilder();
        for (int I = 1; I <= mN; I++) {
            if (MinLevelDict.ContainsKey(I)) {
                sb.AppendLine(MinLevelDict[I].ToString());
            }
            else {
                sb.AppendLine("-1");
            }
        }
        Console.Write(sb.ToString());
    }
}


解説

有向グラフなので、逆辺を貼って、
BFSでゴールからの最短距離を求めてます。