eolymp
bolt
Спробуйте наш новий інтерфейс для відправки розв'язків
Задачі

Series / Parallel Resistor Circuits

Series / Parallel Resistor Circuits

A series/parallel resistor circuit is shown here. \includegraphics{https://static.e-olymp.com/content/6c/6cc13152e284c68489931fe447f06e3e6c58c3f5.jpg} The resistance value is given next to each resistor. Connection points (wires connecting two or more resistors together, are denoted by an uppercase letter. \textbf{A} and \textbf{Z} are reserved for the names of the connection points which are the endpoints of the circuit. Our goal is to calculate the equivalent resistance of the circuit (i.e., the equivalent resistance between \textbf{A} and \textbf{Z}). Within the circuit, a resistor can be specified by a triple consisting of the connection points at either endpoint, and the resistance. The resistor labelled "\textbf{9}" could be specified as either (\textbf{C}, \textbf{D}, \textbf{9}) or (\textbf{D}, \textbf{C}, \textbf{9}). A circuit specification is the set of all resistor specifications. A pair of resistors is in series if one of either of their endpoints have a common connection point that is not use by any other resistor (e.g., the resistors labelled "\textbf{6}" and "\textbf{9}" are both connected to \textbf{C}, which is not connected to anything else). Two series resistors can be replaced by an equivalent single resistor whose resistance is the sum of the replaced resistors (\textbf{15}, in the previous example). A pair of resistors is in parallel if both their endpoints have common connection points (e.g., the resistors labelled "\textbf{3}" and "\textbf{10}" above are both connected to \textbf{R} and \textbf{D}). Two parallel resistors can be replaced by an equivalent single resistor whose resistance is the inverse of the sum of the inverses of the two resistors ((\textbf{1}/\textbf{3} + \textbf{1}/\textbf{10})-\textbf{1} = \textbf{2.307692}, in the previous example). \includegraphics{https://static.e-olymp.com/content/9d/9ddd9a1b637b455af678adc0e4c54fc9af709fad.jpg} The equivalent resistance of a well-formed series-parallel resistor circuit can be determined by successively replacing a series or parallel resistor pair by the single equivalent resistor, until only one is left. Not all circuits can be decomposed into series and parallel components. The Wheatstone Bridge, shown here, is a classic example of a circuit that is not considered a well-formed series-parallel resistor circuit. \includegraphics{https://static.e-olymp.com/content/18/180322ac56d9bfe3e627fc84226b483aabbf46f7.jpg} \InputFile There will be multiple circuit specifications. The first input line for each circuit specification is an integer \textbf{N} (\textbf{N} < \textbf{1000}), the number of resistors in the circuit. This is followed by \textbf{N} lines, each being a resistor specification in the form: \textbf{X} \textbf{Y} \textbf{r} , where \textbf{X} and \textbf{Y} are uppercase characters, and \textbf{r} is a positive integer resistance (\textbf{r} < \textbf{100}). The equivalent resistance is guaranteed never to be greater than \textbf{100}. A circuit with \textbf{N} = \textbf{0} indicates the last circuit, and should not be processed. \OutputFile For each circuit, if the circuit is well-formed and reduces to a single equivalent resistance between \textbf{A} and \textbf{Z}, print the equivalent resistance of the circuit from \textbf{A} to \textbf{Z}, rounded to (and displayed to) \textbf{3} decimal places. If the circuit is not well formed, or if there is no equivalent resistance between \textbf{A} and \textbf{Z}, simply print the number "\textbf{-1.000}". There should be no blank lines between outputs.
Ліміт часу 1 секунда
Ліміт використання пам'яті 64 MiB
Вхідні дані #1
8
N R 2
D R 3
R N 2
R D 10
Z R 7
C D 9
N C 6
A N 4
2
A Z 3
Z A 10
2
P A 6
P Z 9
5
A B 1
B Z 4
A C 8
C Z 19
B C 12
0
Вихідні дані #1
11.945
2.308
15.000
-1.000