Series / Parallel Resistor Circuits

Time limit1sMemory limit128 MB

Summary
Repeatedly merge series and parallel resistor pairs in a given circuit, then report the equivalent resistance between A and Z or -1.000 if it fails.
Level

Medium6 of 10

Topics
Graph, Simulation, Implementation
Solved
No attempts yet

Problem

A series/parallel resistor circuit is shown below.

The resistance value is written next to each resistor. A connection point (a wire joining two or more resistors together) is denoted by an uppercase letter. The letters A and Z are reserved for the two connection points that are the endpoints of the circuit. Our goal is to compute the equivalent resistance of the circuit, i.e., the equivalent resistance between A and Z.

Within the circuit, a resistor can be specified by a triple consisting of the connection points at its two endpoints together with its resistance. For example, the resistor labelled 99 could be specified as either (C,D,9)(C, D, 9) or (D,C,9)(D, C, 9). A circuit specification is the set of all its resistor specifications.

Two resistors are in series if they share a common connection point that is used by no other resistor (for example, resistors 66 and 99 are both connected to CC, and nothing else is connected to CC). Two series resistors can be replaced by a single equivalent resistor whose resistance is the sum of the two (in the example, 1515).

Two resistors are in parallel if both of their endpoints are common connection points (for example, resistors 33 and 1010 are both connected to RR and to DD). Two parallel resistors can be replaced by a single equivalent resistor whose resistance is the inverse of the sum of the inverses of the two resistances (in the example, (13+110)−1=2.307692\left(\frac{1}{3} + \frac{1}{10}\right)^{-1} = 2.307692).

The equivalent resistance of a well-formed series-parallel circuit can be found by repeatedly replacing a series or parallel pair with its single equivalent resistor until only one resistor remains. If this technique gets stuck before reaching a single resistor, the circuit is not well-formed. For instance, a Wheatstone Bridge circuit (shown below) is not a well-formed series-parallel circuit.

Input

There may be several circuit specifications. For each circuit, the first line is an integer NN (N≤1000N \le 1000), the number of resistors in the circuit. It is followed by NN lines, each a resistor specification of the form:

X Y r

where XX and YY are uppercase letters and rr is a positive integer resistance (r<100r < 100). The equivalent resistance is guaranteed never to exceed 100100. A line containing a single 00 terminates the input.

Output

For each circuit, if it is well-formed and reduces to a single equivalent resistance between AA and ZZ, print that equivalent resistance rounded to and shown with exactly 33 decimal places. If the circuit is not well-formed, or if there is no equivalent resistance between AA and ZZ, print -1.000 instead. Do not print blank lines between outputs.

Examples4

  1. Example 1

    Input
    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
    
    Expected output
    11.945
    2.308
    15.000
    -1.000
    
  2. Example 2

    Input
    1
    A Z 5
    0
    
    Expected output
    5.000
    
  3. Example 3

    Input
    2
    A B 3
    B Z 4
    0
    
    Expected output
    7.000
    
  4. Example 4

    Input
    2
    A Z 6
    A Z 6
    0
    
    Expected output
    3.000