Rahyab
Time limit2sMemory limit512 MB
Assign C unit flows from M to T in a directed graph so no flow can unilaterally reroute to pay less, minimizing the sum over flows of the square of their max edge load.
- Level
Hard8 of 10
- Topics
- Graph, Greedy, Math, Shortest path
- Solved
- No attempts yet
Problem
SpongeBob founded a company called Rahyab-Tech after he finished his studies. The company built a mobile app called Rahyab that tells drivers which roads to take between cities. Rahyab caught on, and every driver now uses it on the road.
The hardest case SpongeBob has to handle is the Friday traffic from Motel-Ghu to Tehran. A route starts at Motel-Ghu, passes through some intermediate cities along the roads, and ends at Tehran. Every road is one directional and runs from one city to another city. Roads have no capacity limit. A road wears out faster when more cars drive on it, so the government charges every car by the busiest road of its trip. The surveillance system counts the cars that drive on each road during the day, and a car that drove on roads is charged , where is the number of cars that drove on road that day.
Rahyab does not have to suggest the same route to every driver. Every driver follows the suggested route, but a driver who works out that another route would have cost less drops the app, and SpongeBob cannot accept that. So the plan has to be stable: once the routes are assigned, no driver can pay less by changing route alone while every other driver keeps the assigned route. When a driver moves from route to route , the counts move with the driver. Every road of that is not on carries one more car, every road of that is not on carries one car fewer, and the driver pays the square of the largest count among the roads of .
At the start of the day SpongeBob's program predicts the exact number of cars that travel from Motel-Ghu to Tehran. More than one stable plan can exist, and Rahyab uses a stable plan whose total charge over all cars is as small as possible. Given the map of roads and the number , compute that total charge.
Input
The input holds several test cases. Each test case starts with a line of five space separated integers , , , and . is the number of cities and is the number of roads (, ). The cities are numbered 1 through . is the number of Motel-Ghu and is the number of Tehran (, ). is the number of trips from Motel-Ghu to Tehran on Friday (). Each of the next lines holds two space separated integers and (), a one directional road from city to city . Several roads can join the same pair of cities in the same direction, and each of them is a separate road with its own car count. At least one route from Motel-Ghu to Tehran exists. The input ends with a line holding five zeros, which is not a test case.
Output
For each test case, print one line with the total charge over all cars under a stable plan whose total charge is smallest.