#P3617. 电阻网络

电阻网络

Description

Cjwssb recently encountered a tough problem in physics. He does not know how to compute the equivalent resistance of a circuit. He turned to you for help.

This circuit satisfies the following constraints:

  1. The circuit consists only of wires and resistors of 1Ω1\Omega.
  2. The circuit is connected from left to right, that is, for every resistor or wire with endpoints x,yx,y, it holds that x<yx<y.
  3. Node 11 is the positive terminal of the power supply, and node nn is the negative terminal.
  4. Each node is either unused, or serves as the junction of exactly two branch subcircuits that are connected either in series or in parallel.

Input Format

The first line contains two positive integers n,mn,m, the numbers of nodes and resistors. Nodes are numbered from left to right, so a smaller index is to the left of a larger index.

Each of the next mm lines contains three integers ai,bi,cia_i,b_i,c_i, meaning there is a resistor between nodes aia_i and bib_i with resistance cic_i, where cic_i is either 00 or 11, and for every ii it is guaranteed that ai<bia_i<b_i.

Output Format

Output a real number, the total resistance, rounded to three decimal places.

7 7
1 2 0
1 3 0
2 4 1
3 5 1
4 6 0
5 6 0
6 7 1

1.500

Hint

[Sample Explanation]

Draw the diagram and the answer is obvious.

[Constraints]

Score nn mm
20%20\% n5n\le 5 m5m\le 5
50%50\% n100n\le 100 m120m\le 120
70%70\% n1000n\le 1000 m1200m\le 1200
100%100\% n105n\le 10^5 m1.2×106m\le 1.2\times 10^6

P.S. The testdata are randomly generated under a fixed nn, and the answer is guaranteed not to exceed 10 000.

By: saffah.

Translated by ChatGPT 5