Electrical grids

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≤50002 \le n \le 5000 and the number of wires 0≤m≤10n0 \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 u≠vu \ne v) with capacity cc. Vertices are labeled from 0 to n−1n-1. Vertex 0 corresponds to the power plant and vertex n−1n-1 to the city. Assume 1≤c≤50001 \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.

Problem information

Author: Jordi Petit

Generation: 2026-01-25T10:19:56.032Z

© Jutge.org, 2006–2026.
https://jutge.org