#P6199. [EER1] 河童重工
[EER1] 河童重工
Description
There are locations on Youkai Mountain. The two transportation networks of the Crow Tengu and the Kappa are each made up of some undirected roads connecting these locations, and each road has its own length. If we view these roads as weighted edges, then the two networks can be represented as two trees with nodes, and (a connected undirected weighted graph with edges).
Kappa Heavy Industries now wants to build some new undirected roads. The cost to build a new undirected road is , that is, the sum of the distances from to in and . Kappa Heavy Industries must ensure that any two nodes on Youkai Mountain can reach each other using only the newly built roads. However, since spending too much may trigger an incident, they want the total cost of this project to be as small as possible.
Nitori is the chief designer of this project. Please help her compute the minimum possible total cost of building the new roads.
Input Format
The first line contains a positive integer , representing the number of nodes.
The next lines describe . The -th line contains three integers $x_i, y_i, v_i(1 \leq x_i, y_i \leq n, 1 \leq v_i \leq 5000)$, indicating that there is an edge in of length connecting nodes and .
The next lines describe . The -th line contains three integers $x_j, y_j, v_j(1 \leq x_j, y_j \leq n, 1 \leq v_j \leq 5000)$, indicating that there is an edge in of length connecting nodes and .
Output Format
Output one integer in a single line, representing the minimum total cost.
5
1 2 1
1 3 1
2 4 1
2 5 1
2 3 1
2 4 1
3 5 1
3 1 1
10
4
1 2 1
1 3 1
1 4 1
1 2 1
2 3 1
3 4 1
8
Hint
For of the testdata, .
This problem has subtasks, with the following limits:
Subtask ( points): guarantee .
Subtask ( points): guarantee that and are both chains.
Subtask ( points): guarantee that and are exactly the same except for edge weights (that is, if the two trees are viewed as unweighted trees, then they are the same).
Subtask ( points): guarantee that is a chain.
Subtask ( points): no special constraints.
Translated by ChatGPT 5
京公网安备 11011102002149号