#K86072. Reservoir Overflow Simulation
Reservoir Overflow Simulation
Reservoir Overflow Simulation
You are given a simulation of water distribution among several reservoirs connected by pipes. Each reservoir has a specific capacity and receives water at a constant rate every hour. Additionally, pipes connect pairs of reservoirs and allow water to flow between them up to a maximum flow limit. The water is added first, then transferred via pipes, and after each of these steps, the reservoirs are checked to see if any have exceeded their capacity.
If at any point a reservoir’s water amount strictly exceeds its capacity, the reservoir is said to overflow. Your task is to simulate this process over a given number of hours and determine whether any reservoir will overflow.
The detailed process for each hour is as follows:
- Add the corresponding rate of water to each reservoir.
- Immediately check if any reservoir exceeds its capacity. If so, output "Yes".
- For each pipe (from reservoir u to v with flow limit f), calculate the water difference between reservoir u and v. If the difference is greater than f, transfer exactly f units of water from u to v; otherwise, if the difference is positive, transfer the entire difference. After each transfer, check again for any overflow.
- If no reservoir overflows after processing all pipes for the hour, proceed to the next hour.
If the simulation finishes all hours without any reservoir overflowing, output "No".
inputFormat
The input is given via standard input (stdin) with the following format:
- The first line contains three integers:
R
(the number of reservoirs),P
(the number of pipes), andH
(the number of hours to simulate). - The second line contains
R
space-separated integers representing the capacities of the reservoirs. - The third line contains
R
space-separated integers representing the initial amount of water in each reservoir. - The fourth line contains
R
space-separated integers representing the rate at which water is added to each reservoir per hour. - The following
P
lines each contain three space-separated integers:u
,v
, andf
. Each such line indicates that a pipe connects reservoiru
to reservoirv
with a maximum flow limit off
(reservoir indices are 1-indexed).
outputFormat
Output a single line to standard output (stdout) containing either "Yes" if any reservoir overflows within the given hours, or "No" if none do.
## sample5 4 3
100 80 70 90 60
50 30 40 60 10
10 20 5 15 10
1 2 15
2 3 10
3 4 20
4 5 10
Yes