using System; using System.Collections.Generic; class program { public static void Main() { var str = Console.ReadLine().Split(' '); var N = int.Parse(str[0]); var M = int.Parse(str[1]); var S = int.Parse(str[2]); var G = int.Parse(str[3]); var a = new int[M]; var b = new int[M]; var c = new int[M]; for (var i = 0; i < M; i++) { str = Console.ReadLine().Split(' '); a[i] = int.Parse(str[0]); b[i] = int.Parse(str[1]); c[i] = int.Parse(str[2]); } var dic = new Dictionary(); checkNext(a, b, c, S, G, 0, "", dic, new List()); var min = 1000000000; var bestpath = ""; foreach (KeyValuePair pair in dic) { // Console.WriteLine("{0} {1}", pair.Key, pair.Value); if (pair.Value < min) { bestpath = pair.Key; min = pair.Value; } else if (pair.Value == min) { if (bestpath.CompareTo(pair.Key) > 0) { bestpath = pair.Key; } } } Console.WriteLine(bestpath); } public static void checkNext(int[] a, int[] b, int[] c, int S, int G, int val, string path, Dictionary dic, List passed) { // Console.WriteLine("{0} {1}:{2}",S, G,path); if (S == G) { dic.Add(path + S.ToString(), val); return; } for (var i = 0; i < a.Length; i++) { var flag = false; foreach (var con in passed) { if (flag) break; if (i == con) { flag = true; } } if (flag) continue; if (a[i] == S) { passed.Add(i); checkNext(a, b, c, b[i], G, val + c[i], path + S.ToString() + " ", dic, passed); } else if (b[i] == S) { passed.Add(i); checkNext(a, b, c, a[i], G, val + c[i], path + S.ToString() + " ", dic, passed); } } } }