CCCGuide

Problem S5: Maximum Strategic Savings

2018 · S5 · Original problem · DMOJ

Problem Description

A long time ago in a galaxy far, far away, there are NN planets numbered from 11 to NN. Each planet has MM cities numbered from 11 to MM. Let city ff of planet ee be denoted as (e,f)(e, f).

There are N×PN \times P two-way flights in the galaxy. For every planet ee (1≤e≤N)(1 \le e \le N), there are PP flights numbered from 11 to PP. Flight ii connects cities (e,ai)(e,a_i) and (e,bi)(e,b_i) and costs cic_i energy daily to maintain.

There are M×QM \times Q two-way portals in the galaxy. For all cities with number ff (1≤f≤M)(1 \le f \le M), there are QQ portals numbered from 11 to QQ. Portal jj connects cities (xj,f)(x_j, f) and (yj,f)(y_j, f) and costs zjz_j energy daily to maintain.

It is possible to travel between any two cities in the galaxy using only flights and/or portals.

Hard times have fallen on the galaxy. It was decided that some flights and/or portals should be shut down to save as much energy as possible, but it should remain possible to travel between any two cities afterwards.

What is the maximum sum of energy that can be saved daily?

Input Specification

The first line contains four space-separated integers NN, MM, PP, QQ (1≤N,M,P,Q≤105)(1\le N,M,P,Q \le 10^5).

Then PP lines follow; the ii-th one contains three space-separated integers aia_i, bib_i, cic_i (1≤ai,bi≤M,1≤ci≤108)(1 \le a_i, b_i \le M, 1 \le c_i \le 10^8).

Then QQ lines follow; the jj-th one contains three space-separated integers xjx_j, yjy_j, zjz_j (1≤xj,yj≤N,1≤zj≤108)(1 \le x_j, y_j \le N, 1 \le z_j \le 10^8).

It is guaranteed that it will be possible to travel between any two cities using flights and/or portals. There may be multiple flights/portals between the same pair of cities or a flight/portal between a city and itself.

For 2 of the 15 available marks, P,Q≤100P,Q \le 100 and ci=1c_i = 1 for all 1≤i≤P1 \le i \le P, and zj=1z_j = 1 for all 1≤j≤Q1 \le j \le Q.

For an additional 2 of the 15 available marks, P,Q≤200P,Q \le 200.

For an additional 5 of the 15 available marks, N,M≤200N,M \le 200.

Output Specification

Output a single integer, the maximum sum of energy that can be saved daily.

Sample Input 1

2 2 1 2
1 2 1
2 1 1
2 1 1

Output for Sample Input 1

3

Sample Input 2

2 3 4 1
2 3 5
3 2 7
1 2 6
1 1 8
2 1 5

Output for Sample Input 2

41

Explanation for Output for Sample Input 2

One possible way is to shut down the flights between cities (1,1)(1,1) and (1,1)(1,1), (2,1)(2,1) and (2,1)(2,1), (1,1)(1,1) and (1,2)(1,2), (1,3)(1,3) and (1,2)(1,2), (2,3)(2,3) and (2,2)(2,2), and shut down the portal between cities (2,3)(2,3) and (1,3)(1,3) for total energy savings of 8+8+6+7+7+5=418+8+6+7+7+5=41.