AtCoderのARC
次のARCの問題へ
前のARCの問題へ
ARC056-B 駐車場
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 2");
WillReturn.Add("1 2");
WillReturn.Add("2 3");
WillReturn.Add("1 3");
//1
//2
}
else if (InputPattern == "Input2") {
WillReturn.Add("5 6 5");
WillReturn.Add("1 5");
WillReturn.Add("3 5");
WillReturn.Add("3 2");
WillReturn.Add("4 1");
WillReturn.Add("1 2");
WillReturn.Add("4 3");
//1
//2
//3
//5
}
else if (InputPattern == "Input3") {
WillReturn.Add("5 5 5");
WillReturn.Add("1 4");
WillReturn.Add("4 3");
WillReturn.Add("3 2");
WillReturn.Add("2 5");
WillReturn.Add("5 1");
//1
//2
//5
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static int mN;
static int mS;
static Dictionary<int, List<int>> mEdgeInfoListDict = 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];
mS = wkArr[2];
foreach (string EachStr in InputList.Skip(1)) {
SplitAct(EachStr);
int FromNode = wkArr[0];
int ToNode = wkArr[1];
if (mEdgeInfoListDict.ContainsKey(FromNode) == false) {
mEdgeInfoListDict[FromNode] = new List<int>();
}
if (mEdgeInfoListDict.ContainsKey(ToNode) == false) {
mEdgeInfoListDict[ToNode] = new List<int>();
}
mEdgeInfoListDict[FromNode].Add(ToNode);
mEdgeInfoListDict[ToNode].Add(FromNode);
}
Solve();
}
struct JyoutaiDef
{
internal int CurrNode;
internal int MinNode;
}
static void Solve()
{
var Que = new Queue<JyoutaiDef>();
JyoutaiDef WillEnqueue;
WillEnqueue.CurrNode = mS;
WillEnqueue.MinNode = mS;
Que.Enqueue(WillEnqueue);
// 経路の最小ノードの最大[ノード]なDict
var EdakiriDict = new Dictionary<int, int>();
EdakiriDict[mS] = mS;
while (Que.Count > 0) {
JyoutaiDef Deququed = Que.Dequeue();
if (mEdgeInfoListDict.ContainsKey(Deququed.CurrNode)) {
foreach (int EachToNode in mEdgeInfoListDict[Deququed.CurrNode]) {
WillEnqueue.CurrNode = EachToNode;
WillEnqueue.MinNode = Math.Min(Deququed.MinNode, EachToNode);
if (EdakiriDict.ContainsKey(EachToNode)) {
if (EdakiriDict[EachToNode] >= WillEnqueue.MinNode) {
continue;
}
}
EdakiriDict[EachToNode] = WillEnqueue.MinNode;
Que.Enqueue(WillEnqueue);
}
}
}
var sb = new System.Text.StringBuilder();
foreach (var EachPair in EdakiriDict.OrderBy(pX => pX.Key)) {
if (EachPair.Key <= EachPair.Value) {
sb.AppendLine(EachPair.Key.ToString());
}
}
Console.Write(sb.ToString());
}
}
解説
スタート地点から幅優先探索で、
(ノードごとの、経路の最小ノードの最大)
を求め、
ノード <= (ノードごとの、経路の最小ノードの最大)
を満たすノードを出力してます。