using System; using System.IO; using System.Linq; using System.Collections.Generic; using System.Text; public class Program { public void Proc() { int cityCount = int.Parse(Reader.ReadLine()); int moneyAmount = int.Parse(Reader.ReadLine()); int roadCount = int.Parse(Reader.ReadLine()); int[] fromList = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)-1).ToArray(); int[] toList = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)-1).ToArray(); int[] moneyCostList = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray(); int[] timeCostList = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray(); for (int i = 0; i < roadCount; i++){ Road r = new Road(fromList[i], toList[i], moneyCostList[i], timeCostList[i]); if(!RoadList.ContainsKey(fromList[i])) { RoadList.Add(fromList[i], new Dictionary>()); } if(!RoadList[fromList[i]].ContainsKey(toList[i])) { RoadList[fromList[i]].Add(toList[i], new List()); } RoadList[fromList[i]][toList[i]].Add(r); if(!RoadList.ContainsKey(toList[i])) { RoadList.Add(toList[i], new Dictionary>()); } if(!RoadList[toList[i]].ContainsKey(fromList[i])) { RoadList[toList[i]].Add(fromList[i], new List()); } RoadList[toList[i]][fromList[i]].Add(r); } MapState[] map = new MapState[cityCount]; // 0 から CityCount-1 へ行く Queue q = new Queue(); q.Enqueue(new Task(0,0)); map[0] = new MapState(); map[0].MoneyTime.Add(0, 0); while(q.Count()>0) { Task t = q.Dequeue(); int time = map[t.City].MoneyTime[t.Money]; if(t.City == cityCount-1) { continue; } if(!RoadList.ContainsKey(t.City)) { continue; } foreach(int nextCity in RoadList[t.City].Keys) { if(nextCity == 0) { continue; } foreach(Road r in RoadList[t.City][nextCity]) { int nextTime = time + r.TimeCost; int nextMoney = t.Money + r.MoneyCost; if (nextMoney > moneyAmount) { continue; } if (map[nextCity] != null) { if (map[nextCity].MoneyTime.Any(a => a.Key <= nextMoney && a.Value <= nextTime)) { continue; } if (map[nextCity].MoneyTime.ContainsKey(nextMoney) && map[nextCity].MoneyTime[nextMoney] <= nextTime) { continue; } } if (map[nextCity] == null) { map[nextCity] = new MapState(); } map[nextCity].MoneyTime[nextMoney] = nextTime; q.Enqueue(new Task(nextCity, nextMoney)); } } } int ans = -1; if(map[cityCount-1] !=null) { ans = map[cityCount - 1].MoneyTime.Min(a => a.Value); } Console.WriteLine(ans); } private class Task { public int City; public int Money; public Task(int c,int m) { this.City = c; this.Money = m; } } private class MapState { public Dictionary MoneyTime = new Dictionary(); } private Dictionary>> RoadList = new Dictionary>>(); private class Road { public int City1; public int City2; public int MoneyCost; public int TimeCost; public Road(int city1, int city2, int money, int time) { this.City1 = city1; this.City2 = city2; this.MoneyCost = money; this.TimeCost = time; } } public class Reader { private static StringReader sr; public static bool IsDebug = false; public static string ReadLine() { if (IsDebug) { if (sr == null) { sr = new StringReader(InputText.Trim()); } return sr.ReadLine(); } else { return Console.ReadLine(); } } private static string InputText = @" 41 278 1 3 16 192 606 "; } public static void Main(string[] args) { #if DEBUG Reader.IsDebug = true; #endif Program prg = new Program(); prg.Proc(); } }