luogu#P3617. 电阻网络

电阻网络

Background

What is a resistor? Everyone should know. What is a circuit? You probably know as well. However, in this problem, the definition of a circuit is a bit different:

Every circuit has a positive terminal on the left and a negative terminal on the right. Specifically, circuits are defined as follows:

  • A single 1Ω1\Omega resistor (together with its two endpoints) is a circuit. (Although a wire can be regarded as a 0Ω0\Omega resistor, a wire alone is not a circuit.)
  • If AA and BB are both circuits, let 1,2,31,2,3 be three nodes from left to right. Connect the positive and negative terminals of AA to 11 and 22, respectively, and those of BB to 22 and 33, respectively. Then the part from 11 to 33 is a circuit, with 11 as the positive terminal and 33 as the negative terminal.
  • If AA and BB are both circuits, let 1,2,3,2,3,11,2,3,2',3',1' be six nodes, where 11 is to the left of 22 and 33, 22 is to the left of 22', 33 is to the left of 33', and 22' and 33' are to the left of 11'. There are wires between 11 and 22, between 11 and 33, between 22' and 11', and between 33' and 11'. Connect the positive and negative terminals of AA to 22 and 22', respectively, and those of BB to 33 and 33', respectively. Then the part from 11 to 11' is a circuit, with 11 as the positive terminal and 11' as the negative terminal.

Now you are given a circuit. Find the resistance between its positive and negative terminals.

Problem 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