using System; using System.Collections.Generic; using System.Text; using System.Linq; class Program { public void Proc() { Reader.IsDebug = false; int[] inpt = Reader.GetInt(); int ekiCount = inpt[0]; int roadCount = inpt[1]; this.StartEki = inpt[2]; this.GoalEki = inpt[3]; Dictionary> dic = new Dictionary>(); for(int i=0; i()); } dic[inpt[0]].Add(inpt[1], inpt[2]); if(!dic.ContainsKey(inpt[1])) { dic.Add(inpt[1], new Dictionary()); } dic[inpt[1]].Add(inpt[0], inpt[2]); } Queue task = new Queue(); TaskItem firstTask = new TaskItem(StartEki); task.Enqueue(firstTask); Keiro[] step = new Keiro[ekiCount + 1]; step[StartEki] = firstTask.Route; while (task.Count > 0) { TaskItem item = task.Dequeue(); foreach (int key in dic[item.Pos].Keys) { Keiro kei = item.Route.Duplicate(); kei.Add(key); kei.AddCost(dic[item.Pos][key]); if(step[key] == null || step[key].Cost > kei.Cost) { step[key] = kei; TaskItem tsk = new TaskItem(key, kei); task.Enqueue(tsk); } else if(step[key].Cost == kei.Cost && kei.CompareTo(step[key]) < 0) { step[key] = kei; TaskItem tsk = new TaskItem(key, kei); task.Enqueue(tsk); } } } Console.WriteLine(string.Join(" ", step[GoalEki])); } private int StartEki; private int GoalEki; public class TaskItem { public Keiro Route; public int Pos; public TaskItem(int pos) { this.Route = new Keiro(); this.Route.Add(pos); this.Pos = pos; } public TaskItem(int pos, Keiro kei) { this.Route = kei; this.Pos = pos; } } public class Keiro : List, IComparable> { public long Cost { get { return costValue; } } public void AddCost(long addCost) { this.costValue+=addCost; } private long costValue = 0; public int CompareTo(List other) { for(int i=0; i other[i]) { return 1; } } if(this.Count < other.Count) { return -1; } else if(this.Count > other.Count) { return 1; } return 0; } public Keiro Duplicate() { Keiro newIns = new Keiro(); newIns.AddRange(this); newIns.AddCost(this.costValue); return newIns; } } public class Reader { public static bool IsDebug = true; private static String PlainInput = @" "; private static System.IO.StringReader Sr = null; public static string ReadLine() { if (IsDebug) { if (Sr == null) { Sr = new System.IO.StringReader(PlainInput.Trim()); } return Sr.ReadLine(); } else { return Console.ReadLine(); } } public static int[] GetInt(char delimiter = ' ', bool trim = false) { string inptStr = ReadLine(); if (trim) { inptStr = inptStr.Trim(); } string[] inpt = inptStr.Split(delimiter); int[] ret = new int[inpt.Length]; for (int i = 0; i < inpt.Length; i++) { ret[i] = int.Parse(inpt[i]); } return ret; } } static void Main() { Program prg = new Program(); prg.Proc(); } }