Dragging
Time limit2.5sMemory limit256 MB
Two players take turns dragging each other along a road graph for K hours, one maximizing and one minimizing the saltiness of the final dish. Find that saltiness.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Graph, BFS
- Solved
- No attempts yet
Statement
Taeyoung and Seunghan went abroad for the year-end holiday. While wandering around, they realized that lunchtime was not far away.
Lunchtime starts in exactly hours. The two want to eat at the best restaurant they can reach. The problem is that their food tastes are opposite. Taeyoung likes food that is saltier, and Seunghan likes food that is less salty.
The town has restaurants connected by roads. Every road takes exactly 10 minutes to travel. Two restaurants may be connected by more than one road, and a road may start and end at the same restaurant. Each restaurant sells exactly one dish. The saltiness of the dish at restaurant is the integer .
Neither of them is willing to give in, and their strength is evenly matched. So for the remaining hours, they move according to these rules. For the first 20 minutes, Taeyoung drags Seunghan along. For the next 10 minutes, Seunghan drags Taeyoung along. For the next 10 minutes, Taeyoung drags Seunghan along. For the next 20 minutes, Seunghan drags Taeyoung along. These four steps repeat until hours have passed. For example, with 120 minutes left, the four steps run twice, giving minutes.
During the time a person is dragging the other, that person may stay still. However, at the end of each turn, both must be at a restaurant. They cannot stop halfway along a road.
Both have a map of the town and move under the rules above, choosing whatever is best for themselves. Taeyoung moves toward the dish with the highest saltiness he can reach, and Seunghan moves toward the dish with the lowest saltiness he can reach. They are currently at restaurant 1. Find the saltiness of the dish that Taeyoung and Seunghan end up eating.
Input
The first line contains , , and separated by spaces. (, , )
The second line contains separated by spaces. ()
From the third line through line , each line contains two positive integers and , meaning restaurant and restaurant are connected by a road. ()
Output
Print the saltiness of the dish that Taeyoung and Seunghan end up eating on the first line.