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>(); static void Main() { int targetTown = int.Parse(Console.ReadLine()); int money = int.Parse(Console.ReadLine()); int roadCount = int.Parse(Console.ReadLine()); int[] townStart = (Console.ReadLine()).Split(' ').Select(value => int.Parse(value)).ToArray(); int[] townEnd = (Console.ReadLine()).Split(' ').Select(value => int.Parse(value)).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])); } List timeList = GetTimeList(1, targetTown, money, 0); if (timeList.Count == 0) { timeList.Add(-1); } Console.WriteLine("{0}", timeList.Min()); } private static List GetTimeList(int nowTown, int targetTown, int nowMoney, int nowTime) { List result = new List(); if (nowTown == targetTown) { result.Add(nowTime); } else { foreach (RoadData buffer in roadData[nowTown]) { if ((nowMoney - buffer.cost) >= 0) { result.AddRange(GetTimeList(buffer.nextTown, targetTown, (nowMoney - buffer.cost), (nowTime + buffer.time))); } } } return result; } } }