Evacuation
Time limit2sMemory limit1024 MB
Find the minimum time to send K cars from city 1 to city n over timed, capacitated one-way roads, or the fewest cars that miss the deadline T.
- Level
Hard8 of 10
- Topics
- Graph, Shortest path, Binary search
- Solved
- No attempts yet
Problem
In minutes, the Chitauri army led by Loki attacks Earth. The Avengers cannot stop the portal from opening in New York in time, so Captain America has decided to evacuate all of the city's residents. He needs to find out whether the residents can leave the city before the invasion begins.
The area around New York can be viewed as a set of small cities connected by one-way roads. Each road has a length and a capacity. A length means that a car entering the road at time reaches its far end at time . A capacity means that at most cars can enter the road in any given minute. When a car reaches a city, it can immediately continue onto any road leaving that city, or it can stay in the city for any number of minutes before leaving.
Captain America has already chosen the city where New York's residents must be after the evacuation. He also knows how many cars are needed to evacuate the entire city. Now he must determine whether all residents can evacuate before the invaders arrive. If they can, find the minimum time they need. If they cannot, find the minimum number of cars carrying residents that will not reach the safe city.
Input
The first line contains four integers , , , and (, ): the number of cities around New York, the number of roads between them, the number of cars that must travel from New York to the safe city, and the time until the invasion. The next lines describe the roads.
Each road is described by four integers , , , and (, , , ): the city the road leaves from, the city it leads to, its length, and its capacity.
There is at most one road between any two cities, and it goes in one direction only. New York is city 1, and the safe city is city . At time 0, all cars are in New York.
Output
If all residents can reach the safe city in no more than minutes, print the minimum number of minutes they need. Otherwise, print the minimum number of cars that cannot reach the safe city within minutes. You do not need to say which case applies.