#K76187. Irrigation System Optimization

    ID: 34587 Type: Default 1000ms 256MiB

Irrigation System Optimization

Irrigation System Optimization

You are given a network of farms connected by pipes. Each pipe has a specified capacity. For a given irrigation plan, you must determine whether it is possible to transport a specified amount of water from a source farm to a destination farm without any pipe along the chosen path having a capacity less than the water requirement.

More formally, consider M farms and N pipes. Each pipe is described by three integers: the starting farm, the ending farm, and the pipe's capacity. You will then be given Q irrigation queries. Each query consists of a source, a destination, and the required water amount. For each query, check if there exists a path from the source to the destination such that the minimum capacity of all pipes on that path is at least equal to the required water amount.

This problem may be formulated using the following inequality in each path segment:

min(u,v)pathcuvW,\min_{(u,v)\in \text{path}} c_{uv} \geq W,

where cuv is the capacity of the pipe between farms u and v, and W is the required water for that irrigation plan.

inputFormat

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

M N
u1 v1 c1
u2 v2 c2
... (N lines in total)
Q
s1 t1 w1
s2 t2 w2
... (Q lines in total)

Where:

  • M is the number of farms.
  • N is the number of pipes.
  • Each of the next N lines contains three integers u, v, and c representing a pipe connecting farm u and farm v with capacity c.
  • Q is the number of irrigation queries.
  • Each of the next Q lines contains three integers: the source farm s, the target farm t, and the required water amount w.

outputFormat

For each irrigation query, output a single line to stdout containing either "YES" if the irrigation plan can be executed or "NO" otherwise.

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

YES YES

</p>