Electrical grids P17957


Statement
 

pdf   zip

The electrical grids of the United States and Italy have recently shown deficiencies. To prevent them, Catalan engineers are faced with the following problem.

Electricity must be sent, through several wires, from a power plant where it is produced to a city where it is consumed. Each wire is defined by some capacity and by two vertices that join them to other wires: an input vertex where electricity enters the wire and an output vertex where electricity leaves the wire. Every vertex can distribute electricity as it likes, as long as capacity restrictions are not violated and the amount of electricity that enters the vertex equals the amount of electricity that leaves it.

Which is the greatest amount of electricity shippable from the power plant to the city?

Input

Input consists of several cases. Each case begins with the number of vertices 2n50002 \le n \le 5000 and the number of wires 0m10n0 \le m \le 10n. Follow mm trios of natural numbers uu, vv and cc, to indicate a wire from vertex uu to vertex vv (where uvu \ne v) with capacity cc. Vertices are labeled from 0 to n1n-1. Vertex 0 corresponds to the power plant and vertex n1n-1 to the city. Assume 1c50001 \le c \le 5000 and that for every two vertices uu and vv there is at most one wire from uu to vv.

Output

For every case, print the maximum amount of electricity that can be sent from the power plant to the city.

Public test cases
  • Input

    6 10
    0 1 16   0 2 13   1 2 10   2 1 4
    1 3 12   2 4 14   3 2 9    4 3 7
    3 5 20   4 5 4
    
    2 0
    
    4 6
    0 1 40   1 0 10   0 2 50   1 2 20
    2 1 30   1 3 1000
    
    8 9
    0 1 3
    0 2 2
    1 4 3
    2 3 2
    3 4 2
    1 5 2
    5 6 2
    6 7 2
    4 7 3

    Output

    23
    0
    70
    5
    
  • Information
    Author
    Jordi Petit
    Language
    English
    Official solutions
    C++ Python
    User solutions
    C++ Python