#include struct city_info { int city_num; int money; int road_num; int S_city[1500]; int T_city[1500]; int cost[1500]; int time[1500]; int ans; }; struct city_info city; void numin(int index, int numBox[1500]) { char str; for (int i = 0; i '9') { break; } else numBox[i] = numBox[i] * 10 + (int)str - (int)'0'; } } return; } int search(int S_num, int cost, int timer) { for (int i = 0;i < city.city_num; i++) { if (city.S_city[i] == S_num) { cost += city.cost[i]; timer += city.time[i]; if (city.money < cost) { // コストオーバーの場合は対象外 cost -= city.cost[i]; timer -= city.time[i]; }else if (city.T_city[i] == city.city_num) { // 町まで到着した場合 // 記録より速いか? if (timer < city.ans) city.ans = timer; cost -= city.cost[i]; timer -= city.time[i]; }else { search(city.T_city[i], cost, timer); cost -= city.cost[i]; timer -= city.time[i]; } } } return 0; } int main() { scanf("%d", &city.city_num); scanf("%d", &city.money); scanf("%d", &city.road_num); numin(city.road_num, city.S_city); numin(city.road_num, city.T_city); numin(city.road_num, city.cost); numin(city.road_num, city.time); city.ans = 1500001; search(1, 0, 0); if (city.ans == 1500001) printf("-1"); else printf("%d", city.ans); return 0; }