luogu#P4809. [CCC 2018] 最大战略储备
[CCC 2018] 最大战略储备
Problem Description
This problem is translated from CCC 2018 S5 “Maximum Strategic Savings”.
There are planets, numbered . Each planet has cities, numbered . We denote city on planet as .
There are undirected flight routes. For each planet , there are routes, numbered from to . Route connects cities and , and it costs per day to maintain.
There are undirected ports. For every city index , there are ports, numbered from to . Port can connect cities and , and it costs per day to maintain.
Now you need to remove some ports and/or cancel some flight routes such that all cities are still connected, and the total saved cost is maximized.
Input Format
The first line contains four integers .
The next lines each contain three integers $a_i,\,b_i,\,c_i(1\le a_i,\,b_i\le M,\,1\le c_i\le10^8)$.
The next lines each contain three integers $x_j,\,y_j,\,z_j(1\le x_j,\,y_j\le N,\,1\le z_j\le 10^8)$.
The testdata guarantees that all cities are connected to each other. There may be multiple edges or self-loops.
Output Format
Output one integer on a single line, representing the maximum total cost that can be saved per day.
2 2 1 2
1 2 1
2 1 1
2 1 1
3
2 3 4 1
2 3 5
3 2 7
1 2 6
1 1 8
2 1 5
41
Hint
Explanation for Sample 2
One possible optimal solution is to close the flight routes between cities and , and , and , and , and and ; and close the port between cities and . In the end, you can save a cost of .
For of the testdata, , and for all , ; for all , .
For another of the testdata, .
For another of the testdata, .
For all testdata, .
Translated by ChatGPT 5