Under construction

Still being built. Things may change

Rahyab

Time limit2sMemory limit512 MB

Summary
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 r1,…,rkr_1, \ldots, r_k is charged max⁡{tr12,…,trk2}\max\{t_{r_1}^2, \ldots, t_{r_k}^2\}, where trit_{r_i} is the number of cars that drove on road rir_i 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 PP to route QQ, the counts move with the driver. Every road of QQ that is not on PP carries one more car, every road of PP that is not on QQ carries one car fewer, and the driver pays the square of the largest count among the roads of QQ.

At the start of the day SpongeBob's program predicts the exact number of cars CC 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 CC, compute that total charge.

Input

The input holds several test cases. Each test case starts with a line of five space separated integers NN, EE, MM, TT and CC. NN is the number of cities and EE is the number of roads (2≤N≤5002 \le N \le 500, 0≤E≤1000000 \le E \le 100000). The cities are numbered 1 through NN. MM is the number of Motel-Ghu and TT is the number of Tehran (1≤M,T≤N1 \le M, T \le N, M≠TM \ne T). CC is the number of trips from Motel-Ghu to Tehran on Friday (0≤C≤1060 \le C \le 10^6). Each of the next EE lines holds two space separated integers xix_i and yiy_i (1≤xi,yi≤N1 \le x_i, y_i \le N), a one directional road from city xix_i to city yiy_i. 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.

Examples2

  1. Example 1

    Input
    3 3 1 3 7
    1 3
    1 2
    2 3
    4 5 2 3 6
    1 2
    1 3
    2 4
    4 3
    2 3
    0 0 0 0 0
    
    Expected output
    91
    54
    
  2. Example 2

    Input
    4 4 1 4 0
    1 2
    2 4
    1 3
    3 4
    4 4 1 4 1
    1 2
    2 4
    1 3
    3 4
    0 0 0 0 0
    
    Expected output
    0
    1