Marathon Course
InterviewTime limit2sMemory limit512 MB
Choose a simple path from 1 to N minimizing the cost of its roads, where each road costs C*(P-T)^2 when P > T, and find the largest P whose cost fits budget K.
- Level
Medium7 of 10
- Topics
- Shortest path, Graph, Binary search, Heap
- Solved
- No attempts yet
Problem
A marathon is being held. Runners follow a course fixed in advance, and the course has not been chosen yet. The area of the marathon has intersections and two-way roads. A road connects two intersections. Intersections are numbered to , and roads are numbered to .
A marathon course is written as a list of intersections , where is the number of intersections on the course. The first intersection must always be intersection , and the last intersection must always be intersection . No intersection may appear twice, and two consecutive intersections must be connected by a road.
Holding the marathon requires closing every road on the course. Closing a road may or may not cost money. Each road has a closing cost and a payment threshold . Let be the number of people who take part. A road with is closed at no cost. Closing a road with costs won. The closing cost of a course is the sum of the costs of the roads on it.
The marathon budget pays at most won for road closing. The course is chosen so that as many people as possible can take part. Write a program that finds the largest number of people who can take part when the course is chosen well within the budget.
Input
The first line contains the number of intersections , the number of roads , and the budget . (, , )
Each of the next lines contains one road, given as four integers , , , (, ). and are the two intersections the road connects, and and are the values used to compute the cost of closing that road.
At most one road connects any two intersections, and every input has an answer.
Output
Print on the first line the largest number of people who can take part when the course is chosen well within the budget.
Notes
Suppose a course uses two roads whose values are and . With 3 people the closing cost is won, with 4 people it is won, and with 6 people it is won. With a budget of 25 won, 3 people take part on this course.