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 resistor (together with its two endpoints) is a circuit. (Although a wire can be regarded as a resistor, a wire alone is not a circuit.)
- If and are both circuits, let be three nodes from left to right. Connect the positive and negative terminals of to and , respectively, and those of to and , respectively. Then the part from to is a circuit, with as the positive terminal and as the negative terminal.
- If and are both circuits, let be six nodes, where is to the left of and , is to the left of , is to the left of , and and are to the left of . There are wires between and , between and , between and , and between and . Connect the positive and negative terminals of to and , respectively, and those of to and , respectively. Then the part from to is a circuit, with as the positive terminal and 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:
- The circuit consists only of wires and resistors of .
- The circuit is connected from left to right, that is, for every resistor or wire with endpoints , it holds that .
- Node is the positive terminal of the power supply, and node is the negative terminal.
- 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 , 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 lines contains three integers , meaning there is a resistor between nodes and with resistance , where is either or , and for every it is guaranteed that .
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 | ||
|---|---|---|
P.S. The testdata are randomly generated under a fixed , and the answer is guaranteed not to exceed 10 000.
By: saffah.
Translated by ChatGPT 5