Electrical grids P17957


Statement
 

pdf   zip

html

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 2 ≤ n ≤ 5000 and the number of wires 0 ≤ m ≤ 10n. Follow m trios of natural numbers u, v and c, to indicate a wire from vertex u to vertex v (where uv) with capacity c. Vertices are labeled from 0 to n−1. Vertex 0 corresponds to the power plant and vertex n−1 to the city. Assume 1 ≤ c ≤ 5000 and that for every two vertices u and v there is at most one wire from u to v.

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