#P4499. Bridge Sum in an Infinite Layered Graph

    ID: 17745 Type: Default 1000ms 256MiB

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 ii (i1i\ge1) contains nn nodes numbered from 11 to nn, denoted as (i,x)(i, x) for 1xn1 \le x \le n. The graph is constructed according to the following rules:

  1. The graph is connected.

  2. 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.

  3. For every edge between two nodes in the same layer, say an edge of weight dd connecting (i,x)(i,x) and (i,y)(i,y), the following property holds: for every positive integer jij\ge i, there is an edge between (j,x)(j,x) and (j,y)(j,y) with weight 0.9jid0.9^{j-i} \cdot d.

  4. For every edge between two consecutive layers, say an edge of weight dd connecting (i,x)(i,x) and (i+1,y)(i+1,y), for every positive integer jij\ge i, there is an edge connecting (j,x)(j,x) and (j+1,y)(j+1,y) with weight 0.9jid0.9^{j-i} \cdot d.

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 dd 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:

  • nn: the number of nodes in each layer,
  • m1m_1: the number of intra-layer edges (edges within each layer), and
  • m2m_2: the number of inter-layer edges (edges between layer ii and layer i+1i+1).

Then follow m1m_1 lines, each containing three numbers uu, vv, dd (with 1u,vn1\le u,v\le n) describing an intra-layer edge in the first layer having weight dd. For each such edge, for every layer i1i\ge1 there is an edge between (i,u)(i, u) and (i,v)(i, v) with weight 0.9i1d0.9^{i-1}d.

Then follow m2m_2 lines, each containing three numbers xx, yy, dd (with 1x,yn1\le x,y\le n) describing an inter-layer edge between layer ii and layer i+1i+1 in the blueprint (for i=1i=1); for every i1i\ge1, there is an edge between (i,x)(i, x) and (i+1,y)(i+1, y) with weight 0.9i1d0.9^{i-1}d.

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 1,2,,n1,2,\dots, n representing layer 1 and vertices n+1,n+2,,2nn+1, n+2,\dots,2n representing layer 2. For each intra-layer edge in the blueprint (given with weight dd), add an edge connecting uu and vv in layer 1 with weight dd, and another edge connecting n+un+u and n+vn+v in layer 2 with weight 0.9d0.9d. For each inter-layer edge (with weight dd), add an edge connecting vertex xx in layer 1 and vertex n+yn+y in layer 2 with weight dd.

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 10d10d (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 0.10.1, its total contribution will be 11, and you should output 1.

inputFormat

The first line contains three integers nn, m1m_1, m2m_2.

Following this, there are m1m_1 lines; each line contains two integers uu and vv and a real number dd describing an intra-layer edge in the first layer.

Then there are m2m_2 lines; each line contains two integers xx and yy and a real number dd describing an inter-layer edge (from layer ii to layer i+1i+1 in the blueprint).

It is guaranteed that 1u,v,x,yn1\le u,v,x,y\le n.

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