#K10191. Max Water Delivery

    ID: 23192 Type: Default 1000ms 256MiB

Max Water Delivery

Max Water Delivery

In this problem, you are given a network of farms and canals, where the reservoir is located at node 1. Each canal segment is represented by an edge with a given capacity. Your task is to compute the maximum amount of water that can be delivered from the reservoir to a specified target farm using the canals.

The network is modeled as a directed graph where:

  • n is the number of nodes (including the reservoir and all farms).
  • m is the number of canal segments.
  • Each canal is represented by a triple \( (u, v, c) \) indicating there is a canal from node \( u \) to node \( v \) with capacity \( c \).

You are required to compute the maximum flow from the reservoir (node 1) to the target node \( t \) using the Edmonds-Karp algorithm, which is an implementation of the Ford-Fulkerson method that uses BFS to find the shortest augmenting path.

Note: If the target node is not reachable from the reservoir, the answer is 0.

inputFormat

The input is read from standard input (stdin) and has the following format:

n m t
u1 v1 c1
u2 v2 c2
... (m lines)

Where:

  • n: Number of nodes.
  • m: Number of canal segments.
  • t: The target node.
  • Each of the next m lines contains three integers: u, v, c, representing a canal from node u to node v with capacity c.

outputFormat

The output should be written to standard output (stdout) and is a single integer representing the maximum water flow deliverable from the reservoir (node 1) to the target node t.

## sample
4 5 4
1 2 40
1 3 20
2 3 10
2 4 30
3 4 20
50