Easter Holidays Ski Journey
Time limit1sMemory limit128 MB
For each resort, find a cycle that goes up by lifts then down by slopes, maximizing the ratio of total slope time to total lift time, and print it as a reduced fraction.
- Level
Hard8 of 10
- Topics
- Binary search, Shortest path, Graph, Math
- Solved
- No attempts yet
Problem
Scandinavians love to ski during the Easter holidays at a large ski resort. The resort has many lifts that carry skiers upward and slopes of various difficulty that are skied downward.
Per is a beginner and is afraid of lifts, yet he still wants to ski as much as possible. He wants to plan a ski journey that:
- starts at the bottom of some lift and returns to that same place;
- consists of exactly two phases: first he rides one or more lifts upward, then he skis all the way back down to the starting place using slopes only;
- is as little scary as possible, meaning the ratio of the time spent skiing on slopes to the time spent riding or waiting for lifts is as large as possible.
A resort has places, slopes, and lifts (, , ). Each slope leads from a higher place to a lower place, and each lift leads from a lower place to a higher place (a lift cannot be ridden downward). It is guaranteed that at least one valid ski journey exists in every resort.
Input
The first line contains the number of resorts to process. Each resort is described as follows. The first line contains three integers , , and . The next lines each describe a slope with three integers: its upper place, its lower place (places are numbered from to ), and the time to ski down the slope (at most ). The following lines each describe a lift with three integers: its lower place, its upper place, and the time to wait for and ride the lift up (at most ). No two places are connected by more than one lift or by more than one slope.
Output
For each resort, print on a single line the largest achievable scariness ratio as an irreducible fraction . This ratio is the total time spent on slopes divided by the total time spent riding or waiting for lifts.