#include #define MAX 1500 #define A 51 typedef struct{ int exists; int cost; int time; }cost_t; typedef struct{ int exists; cost_t costs[51]; }path_t; int get_time(int cost_limit, int start, int via, int goal, cost_t total, path_t pathes[]){ int i; int min = -1; if(via == goal){ if(total.cost <= cost_limit){ return total.time; } else{ return -1; } } for(i = 0;i < A;i++){ if(pathes[via].exists && pathes[via].costs[i].exists){ cost_t c; c.cost = pathes[via].costs[i].cost + total.cost; c.time = pathes[via].costs[i].time + total.time; int tmp = get_time(cost_limit, via, i, goal, c, pathes); if((min == -1) || ((tmp != -1) && (tmp < min))){ min = tmp; } } } return min; } int solve(int cost_limit, int goal, path_t pathes[]){ int i, ans = -1; cost_t c = {0}; ans = get_time(cost_limit, 1, 1, goal, c, pathes); return ans; } int main(){ int i, n, c, v; int tmp[4][MAX]; path_t pathes[A]; for(i = 0;i < A;i++){ int j; pathes[i].exists = 0; for(j = 0;j < A;j++){ pathes[i].costs[j].exists = 0; pathes[i].costs[j].cost = -1; pathes[i].costs[j].time = -1; } } scanf("%d %d %d", &n, &c, &v); for(i = 0;i < 4;i++){ int j; for(j = 0;j < v;j++){ scanf("%d", &tmp[i][j]); } } for(i = 0;i < v;i++){ int start = tmp[0][i]; int goal = tmp[1][i]; int cost = tmp[2][i]; int time = tmp[3][i]; //printf("start=%d, goal=%d, cost=%d, time=%d\n", start, goal, cost, time); pathes[start].exists = 1; pathes[start].costs[goal].exists = 1; pathes[start].costs[goal].cost = cost; pathes[start].costs[goal].time = time; } printf("%d\n", solve(c, n, pathes)); return 0; }