using System; using System.Collections.Generic; using System.Linq; namespace YukiCoderNo1 { class Program { private class RoadData { public int nextTown { get; private set; } public int cost { get; private set; } public int time { get; private set; } public RoadData(int nextTown, int cost, int time) { this.nextTown = nextTown; this.cost = cost; this.time = time; } } private static Dictionary> roadData = new Dictionary>(); private static int minTime = int.MaxValue; static void Main() { int targetTown = int.Parse(Console.ReadLine()) - 1; int money = int.Parse(Console.ReadLine()); int roadCount = int.Parse(Console.ReadLine()); int[] townStart = (Console.ReadLine()).Split(' ').Select(value => int.Parse(value) - 1).ToArray(); int[] townEnd = (Console.ReadLine()).Split(' ').Select(value => int.Parse(value) - 1).ToArray(); int[] cost = (Console.ReadLine()).Split(' ').Select(value => int.Parse(value)).ToArray(); int[] time = (Console.ReadLine()).Split(' ').Select(value => int.Parse(value)).ToArray(); for (int i = 0; i < roadCount; i++) { roadData.Add(i, new List()); } for (int i = 0; i < roadCount; i++) { roadData[townStart[i]].Add(new RoadData(townEnd[i], cost[i], time[i])); } GetTimeList(0, targetTown, money, 0); if (minTime == int.MaxValue) { minTime = -1; } Console.WriteLine("{0}", minTime); } private static void GetTimeList(int nowTown, int targetTown, int nowMoney, int nowTime) { if (nowTown == targetTown) { if (nowTime < minTime) { minTime = nowTime; } } else { foreach (RoadData buffer in roadData[nowTown]) { if ((nowMoney - buffer.cost) >= 0) { if (nowTime < minTime) { GetTimeList(buffer.nextTown, targetTown, (nowMoney - buffer.cost), (nowTime + buffer.time)); } } } } } } }