Cartesian MST

아직 제출이 없습니다시간 제한1초메모리 제한512 MB

문제

Let GG and HH be two weighted undirected simple graphs. We define the cartesian product of the two graphs, GHG \square H, as the graph whose vertex set is the cartesian set product of the vertex sets of the two graphs V(G)×V(H)V(G) \times V(H) and in which there is an edge between vertices (u_1,v_1)(u\_1, v\_1) and (u_2,v_2)(u\_2, v\_2) if and only if:

  • v_1=v_2v\_1 = v\_2 and there is an edge (u_1,u_2)(u\_1, u\_2) in GG. In this case, the edge((u_1,v_1),(u_2,v_2))((u\_1, v\_1), (u\_2, v\_2)) in GHG \square H has the same weight as the edge (u_1,u_2)(u\_1, u\_2) in GG.
  • or u_1=u_2u\_1 = u\_2 and there is an edge (v_1,v_2)(v\_1, v\_2) in HH. In this case, the edge((u_1,v_1),(u_2,v_2))((u\_1, v\_1), (u\_2, v\_2)) in GHG \square H has the same weight as the edge (v_1,v_2)(v\_1, v\_2) in HH.

You are given two connected graphs GG and HH. Compute the total weight of the minimum spanning tree of GHG \square H.

입력

The first line contains four integers n_1,m_1,n_2,m_2n\_1, m\_1, n\_2, m\_2 (2n_1,n_21052 \leq n\_1, n\_2 \leq 10^5; 1m_1,m_21051 \leq m\_1, m\_2 \leq 10^5): the number of vertices of GG, the number of edges of GG, the number of vertices of HH, and the number of edges of HH, respectively. 

Each of the next m_1m\_1 lines contains three integers u_i,v_i,w_iu\_i, v\_i, w\_i (0u_i,v_in_110 \leq u\_i, v\_i \leq n\_1 - 1; 1w_i1081 \leq w\_i \leq 10^8), describing an edge of GG between vertices u_iu\_i and v_iv\_i with weight w_iw\_i.

Each of the next m_2m\_2 lines contains three integers u_i,v_i,w_iu\_i, v\_i, w\_i (0u_i,v_in_210 \leq u\_i, v\_i \leq n\_2 - 1; 1w_i1081 \leq w\_i \leq 10^8), describing an edge of HH between vertices u_iu\_i and v_iv\_i with weight w_iw\_i.

It is guaranteed that graphs GG and HH are simple and connected. Recall that a graph is simple if there are no edges between a vertex and itself, and there is at most one edge between any two vertices.

출력

Output one integer: the weight of the minimum spanning tree of GHG \square H.