Equivalent Resistance of a Resistor Network

No attempts yetTime limit1sMemory limit128 MB

Problem

A common component in electronic circuits is the resistor. Each resistor has two terminals, and when current flows through it, some of the current is converted to heat, thus "resisting" its flow. How strongly it does this is given by a single positive number called the resistance of the resistor, measured in Ohms.

When two resistors are connected in series, their equivalent resistance is simply the sum of the individual resistances. For example, a 100 Ohm resistor and a 200 Ohm resistor in series combine to 300 Ohms. Connecting three or more resistors in series likewise gives the sum of all their resistances.

When resistors are connected in parallel, the equivalent resistance $R$ satisfies

$$\frac{1}{R} = \frac{1}{R_1} + \frac{1}{R_2} + \cdots$$

For example, a 100 Ohm and a 150 Ohm resistor in parallel give $\frac{1}{\frac{1}{100} + \frac{1}{150}} = 60$ Ohms, while a 100 Ohm, 150 Ohm, and 300 Ohm resistor in parallel give $\frac{1}{\frac{1}{100} + \frac{1}{150} + \frac{1}{300}} = 50$ Ohms.

You are given one or more descriptions of resistors and how they are interconnected. Each interconnection point (a terminal of a resistor) is identified by a unique positive integer label. Each resistor is described by the labels of its two terminals and its resistance (a real number). For instance,

1 2 100

means a 100 Ohm resistor is connected between points 1 and 2. A pair of resistors in series might be described as

1 2 100
2 3 200

(a 100 Ohm resistor between points 1 and 2, and a 200 Ohm resistor between points 2 and 3), and two resistors in parallel as

1 2 100
1 2 150

Given the interconnections and the resistance of every resistor, determine the equivalent resistance between two specified points using the rules above. Every network in this problem can be solved by repeatedly applying the series and parallel rules.

Input

The input contains one or more cases. Each case begins with a line of three integers $N$, $A$, and $B$: $A$ and $B$ are the labels of the two points between which you must find the equivalent resistance, and $N$ is the number of resistors, with $N \le 30$. The line 0 0 0 follows the last case and must not be processed. After the N A B line come $N$ lines, each giving the two terminal labels of a resistor and its resistance as a real number.

Output

For each case, print one line containing the case number (cases are numbered sequentially starting from 1) and the equivalent resistance rounded to exactly two decimal places, in the format:

Case k: R Ohms

Notes

  1. Some resistors may not contribute to the resistance between the two given points. For example, in the last sample case (the six-resistor network), the resistor between points 1 and 2 carries no current and is unused. A resistor contributes only if some current can flow through it.
  2. No resistor connects a point to itself; the two terminal labels of a resistor are always distinct.