#P4499. Bridge Sum in an Infinite Layered Graph
Bridge Sum in an Infinite Layered Graph
Bridge Sum in an Infinite Layered Graph
We are given an infinite undirected graph whose nodes are arranged in infinitely many layers. Each layer () contains nodes numbered from to , denoted as for . The graph is constructed according to the following rules:
-
The graph is connected.
-
There are two types of edges, and all edges are undirected. In each layer, some pairs of nodes are connected. Moreover, some nodes in consecutive layers are connected.
-
For every edge between two nodes in the same layer, say an edge of weight connecting and , the following property holds: for every positive integer , there is an edge between and with weight .
-
For every edge between two consecutive layers, say an edge of weight connecting and , for every positive integer , there is an edge connecting and with weight .
A (base) edge (i.e. the family generated by a single input edge) is called a bridge if, after removing all of its copies from the graph, the entire infinite graph becomes disconnected. Notice that if a base edge with original weight is a bridge, then its copies appear in every applicable layer and their total weight forms a geometric series with sum [ \frac{d}{1-0.9} = 10,d. ]
Your task is to determine the sum of weights of all bridges in the infinite graph given the finite blueprint. It is guaranteed that the infinite graph generated by the blueprint is connected.
The input is given as follows. The first line contains three integers:
- : the number of nodes in each layer,
- : the number of intra-layer edges (edges within each layer), and
- : the number of inter-layer edges (edges between layer and layer ).
Then follow lines, each containing three numbers , , (with ) describing an intra-layer edge in the first layer having weight . For each such edge, for every layer there is an edge between and with weight .
Then follow lines, each containing three numbers , , (with ) describing an inter-layer edge between layer and layer in the blueprint (for ); for every , there is an edge between and with weight .
A base edge (either intra- or inter-layer) is a bridge if after removing all of its copies from the infinite graph the graph becomes disconnected. In this problem, to decide whether a base edge is a bridge, you can work with a finite mosaic graph constructed from two consecutive layers as follows:
Construct a graph with vertices representing layer 1 and vertices representing layer 2. For each intra-layer edge in the blueprint (given with weight ), add an edge connecting and in layer 1 with weight , and another edge connecting and in layer 2 with weight . For each inter-layer edge (with weight ), add an edge connecting vertex in layer 1 and vertex in layer 2 with weight .
A base edge is a bridge if, when you remove all its copies (i.e. for an intra-layer edge remove both copies; for an inter-layer edge remove its single copy from the mosaic), the mosaic graph becomes disconnected. The contribution of a bridge is then (because of the infinite geometric series).
Output the sum of the weights of all bridges in the infinite graph. For example, if the only bridge has an original weight , its total contribution will be , and you should output 1.
inputFormat
The first line contains three integers , , .
Following this, there are lines; each line contains two integers and and a real number describing an intra-layer edge in the first layer.
Then there are lines; each line contains two integers and and a real number describing an inter-layer edge (from layer to layer in the blueprint).
It is guaranteed that .
outputFormat
Output a single number, which is the sum of the weights of all bridges in the infinite graph. The answer will be accepted if it is correct within a reasonable error tolerance.
sample
2 1 1
1 2 1
1 1 1
20.0