Road Planner

Time limit1sMemory limit128 MB

Summary
Given a DAG with linear latency functions per edge, distribute an integer number of cars from source to sink to reach Wardrop (selfish routing) equilibrium and output the floor of the equilibrium travel time.
Level

Hard9 of 10

Topics
Graph, Math, Binary search
Solved
No attempts yet

Problem

An acyclic road network is composed of unidirectional road segments (edges), each connecting two intersections (vertices) out of a total of NN intersections. The time necessary to travel on a road segment is always positive and it depends linearly on the number of cars CC using the segment ss:

T(s)=as⋅C+bsT(s) = a_s \cdot C + b_s

asa_s and bsb_s are two constants expressed as single precision floating point numbers, describing the properties of road segment ss. The time necessary to cross an intersection is 00.

A number of cars must travel from vertex 00 to vertex N−1N-1 in this road network. Each car chooses its route selfishly in order to reduce its own travel time; each car knows that all the other cars will also selfishly choose their route. You are asked to write a program that computes how many cars will travel on each of the possible paths from the source to the destination, and the time needed to travel on those paths.

Input

The input contains several tests and is organized as follows. The first line contains the number of tests. The following lines contain the specification of the tests. Each test contains on the first line the number of vertices, the number of edges and the number of cars, separated by whitespaces. After that, each edge is given on a separate line and contains the following fields separated by spaces: source vertex, destination vertex, asa_s and bsb_s.

Output

The output will contain one line for each test input, specifying the minimum time for any car to travel in the network, rounded down to the nearest integer.

Hint

The first input consists of 44 vertices and 44 edges; 40004000 cars must travel from vertex 00 to vertex 33. To achieve the smallest travel time, cars will choose one of the two possible paths (0→1→3)(0 \to 1 \to 3) and (0→2→3)(0 \to 2 \to 3) such that the number of cars on both is equal; hence the minimum travel time is ⌊0.01×2000+45.1⌋=65\lfloor 0.01 \times 2000 + 45.1 \rfloor = 65.

The second input is similar to the first one, except for a zero-cost edge that is added between vertices 11 and 22. In this case, all cars selfishly choose to take the path (0→1→2→3)(0 \to 1 \to 2 \to 3) and the minimum travel time is 8080.

Examples1

  1. Example 1

    Input
    2
    4 4 4000
    0 1 0.01 0
    0 2 0 45.1
    1 3 0 45.1
    2 3 0.01 0
    4 5 4000
    0 1 0.01 0
    0 2 0 45.1
    1 3 0 45.1
    1 2 0 0
    2 3 0.01 0
    
    Expected output
    65
    80