#include #include #include #include #include using namespace std; class Route { public: int town1; int town2; int cost; int time; Route() { } Route(const Route& src) : town1(src.town1), town2(src.town2), cost(src.cost), time(src.time) { } Route(int town1, int town2, int cost, int time) : town1(town1), town2(town2), cost(cost), time(time) { } Route& operator=(const Route& src) { if (&src != this) { town1 = src.town1; town2 = src.town2; cost = src.cost; time = src.time; } return *this; } }; int N; int C; int V; vector routes; bool input(istream& in) { if (!(in >> N)) return false; in >> C; in >> V; routes.resize(V); for (int i = 0; i < V; i++) in >> routes[i].town1; for (int i = 0; i < V; i++) in >> routes[i].town2; for (int i = 0; i < V; i++) in >> routes[i].cost; for (int i = 0; i < V; i++) in >> routes[i].time; return true; } class RouteState { public: int cost; int time; RouteState() : cost(0), time(0) { } RouteState(const RouteState& src) : cost(src.cost), time(src.time) { } RouteState(int cost, int time) : cost(cost), time(time) { } RouteState& operator=(const RouteState& src) { if (&src != this) { cost = src.cost; time = src.time; } return *this; } }; struct less_RouteState { bool operator()(const RouteState& a, const RouteState& b) { return a.time < b.time; } }; int resolve() { vector> route_indexes(N); for (int i = 0; i < V; i++) { route_indexes[routes[i].town1 - 1].push_back(i); } vector< vector > town_state(N); town_state[0].push_back(RouteState(0, 0)); for (int src_town = 0; src_town < N; src_town++) { for (auto& src_town_state : town_state[src_town]) { for (auto& route_index : route_indexes[src_town]) { auto& route = routes[route_index]; int next_cost = src_town_state.cost + route.cost; int next_time = src_town_state.time + route.time; if (next_cost <= C) { auto& dst_town_state = town_state[route.town2 - 1]; bool can_add = true; for (auto& town_to_item : dst_town_state) { if (next_cost > town_to_item.cost && next_time > town_to_item.time) { can_add = false; break; } if (next_cost < town_to_item.cost && next_time < town_to_item.time) { town_to_item.cost = next_cost; town_to_item.time = next_time; can_add = false; break; } } if (can_add) { dst_town_state.push_back(RouteState(next_cost, next_time)); } } } } } auto& last_town = town_state.back(); auto ite = min_element(last_town.begin(), last_town.end(), less_RouteState()); if (ite != town_state.back().end()) { return ite->time; } return -1; } int main(int argc, char **argv) { if (argc > 1) { ifstream stream(argv[1]); while (input(stream)) { cout << resolve() << endl; } } else { while (input(cin)) { cout << resolve() << endl; } } }