Strike
Time limit1sMemory limit128 MB
Choose one train to hold for k minutes in a DAG rail network so the total delay passed on to all trains is largest.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Topological sort, Graph
- Solved
- No attempts yet
Problem
Byteland is proud to own the largest lignite (brown coal) mine in the world. Every day, coal from the mine is carried across the railway network to every city in Byteland so that residents have something to burn in their stoves.
Transport works like this: first, some trains set out from the city with the mine toward a few other cities; then from those cities more trains depart to still other cities, and so on. For every city in Byteland there is at least one sequence of trains such that coal from the mine is loaded onto train , then for each the coal is transferred from train to train , until it finally reaches that city on train . Several trains may arrive at any city (except the city with the mine), but there are no cycles: once you board a train in some city, you can never return to that city by rail.
The trains are coordinated: departure times are set so that a train leaving a city departs only after every scheduled coal train has arrived at that city. If a train is delayed, it may in turn delay other trains. The railway workers are planning a strike: they can hold exactly one train for minutes. They want to pick the train so that the total delay over all trains is as large as possible.
Compute that maximum total delay.
Input
The first line contains two integers and (, ): the number of cities in Byteland and the number of direct rail connections. The second line contains one integer (): the number of minutes for which the workers can hold one train. Cities are numbered from to ; the mine is in city .
Each of the next lines contains four integers , , , (, , ). They mean that, on schedule, the -th train leaves city exactly minutes after sunrise and arrives at city exactly minutes later, on the same day (a Byteland day lasts minutes). For every city, the departure times of the trains leaving it are not smaller than the largest arrival time of any train arriving at it.
Output
Print a single integer: the maximum total delay of the trains that the workers' strike can cause.
Hint
For example, holding for minutes the train that runs from city to city delays that train together with the two trains departing from city .