#K36347. Calibrating Subsystems under Energy Constraints

    ID: 25734 Type: Default 1000ms 256MiB

Calibrating Subsystems under Energy Constraints

Calibrating Subsystems under Energy Constraints

You are given a set of n subsystems and an energy budget E. Some subsystems depend on others: each dependency is represented as a triple \( (a, b, e) \), which means that subsystem \( a \) must be calibrated before subsystem \( b \) and that calibrating subsystem \( a \) incurs an energy cost of \( e \) units. The energy cost for a subsystem is defined as the sum of all \( e \) for dependencies leaving it. During calibration, only subsystems with no pending prerequisites may be processed, and as each is calibrated, its energy cost is added to the total energy consumed and its corresponding costs are removed from its dependent subsystems.

Your task is to determine whether it is possible to calibrate all subsystems without exceeding the energy budget \( E \). If it is possible, you must output a valid calibration sequence. Otherwise, output NO.

The key condition is that for each subsystem \( i \) being calibrated, the following must hold:

\( \text{totalEnergy} + \text{energyCost}[i] \le E \)

inputFormat

The first line of input contains three integers ( n ), ( E ), and ( m ) where:

  • \( n \) is the number of subsystems,
  • \( E \) is the energy budget, and
  • \( m \) is the number of dependencies.
Each of the following \( m \) lines contains three integers \( a \), \( b \), and \( e \), indicating that subsystem \( a \) must be calibrated before subsystem \( b \), and the process incurs an energy cost of \( e \) units.

outputFormat

If it is impossible to calibrate all subsystems within the energy budget, output a single line containing NO. Otherwise, output YES on the first line, followed by a second line with ( n ) space-separated integers representing a valid calibration sequence.## sample

4 10 4
1 2 3
2 3 2
3 4 4
2 4 1
YES

1 2 3 4

</p>