#P3617. Calculate the Total Resistance in a Series‐Parallel Circuit
Calculate the Total Resistance in a Series‐Parallel Circuit
Calculate the Total Resistance in a Series‐Parallel Circuit
Cjwssb is facing a difficult problem in physics: he cannot compute the total resistance of an electrical circuit. The circuit conforms to the following restrictions:
- The circuit is made up only of wires and resistors (each resistor has a resistance of 1 ω).
- The circuit is connected from left to right; that is, for every component connecting two nodes x and y, it holds that x < y.
- Terminal 1 is the positive pole of the power supply and terminal n is the negative pole.
- Each terminal (or connection point) is connected either to two branch circuits (via series or parallel connection) or not connected to any resistor/wire.
Your task is to compute the effective resistance between terminal 1 and terminal n of the given circuit. The resistors obey the following rules:
- Series combination: If two resistors of resistances \(R_1\) and \(R_2\) are connected in series, the equivalent resistance is \[ R = R_1 + R_2. \]
- Parallel combination: If they are connected in parallel, then the equivalent resistance is defined by \[ \frac{1}{R} = \frac{1}{R_1} + \frac{1}{R_2}, \quad\text{or}\quad R = \frac{1}{\frac{1}{R_1} + \frac{1}{R_2}}. \]
Note that wires (ideal conductors with 0 ω resistance) will short‐circuit the circuit. In other words, if after contracting wires the positive and negative terminals become connected, the effective resistance is 0 ω.
inputFormat
The first line contains two integers n and m where:
- n (\(2 \le n \le 10^5\)) is the number of terminals (nodes).
- m is the number of circuit elements.
Each of the following m lines contains three integers x, y and t (with x < y), where:
- x and y denote the two connected terminals.
- t is the type of the element: 0 represents a wire (0 ω) and 1 represents a resistor (1 ω).
It is guaranteed that terminal 1 is the positive pole and terminal n is the negative pole.
outputFormat
Output the effective resistance between terminal 1 and terminal n as a floating‐point number rounded to two decimal places.
sample
2 1
1 2 1
1.00
</p>