#P5630. 【AFOI-19】跳闸
【AFOI-19】跳闸
Description
IY and SY found that the main breaker circuit was completely damaged, so they had to rebuild a circuit.
There are current-conducting nodes in the lab. Each node can be connected to other nodes by wires. Nodes that are connected can transmit current to each other.
Because of reserved-space limitations, some nodes cannot be connected directly. Now IY and SY know there are pairs of nodes that can be connected directly, and they also know the wire length needed to connect each pair.
Having only conducting nodes is not enough. SY took out her long-kept power generator. The generator can be attached to a node and supply power to that node. However, the generator has some limitations: it can only be attached to node , and it has only ports, meaning the attached node can connect to at most wires. Also, due to linkage effects, the generator will supply power only if all its ports are connected to wires.
IY and SY’s goal is to make every node receive power. They need wires, but the longer the wire, the price grows exponentially. So they want to make the longest wire as short as possible.
SY is responsible for laying the wires, and IY is assigned to buy the wires, so he needs to know the total length of wire he has to buy. Since SY is busy building the circuit, IY also has to answer each of SY’s queries: what is the total wire length required to make node and node connected. But IY is too weak and does not know the answers.
Please help the weak IY answer these questions. As a reward, this problem will give you full marks.
Input Format
The first line contains four integers , meaning there are nodes, pairs of nodes that can be directly connected, the power generator has ports, and the generator is attached to node .
The next lines each contain three integers , meaning and can be directly connected by a wire, and the required wire length between them is .
The next line contains an integer , meaning SY has queries.
The next lines each contain two integers , meaning SY asks IY: how long a wire (in total) is needed to make node and node connected.
Output Format
Output one integer on the first line, representing the total length of all wires.
Then output lines, each containing one integer, representing the answer to each of SY’s queries.
There may be cases with no solution. If there is no solution, output "can not fix it.", and output nothing else.
5 7 1 1
1 3 1
2 1 5
4 2 3
2 5 4
5 1 2
3 5 7
4 1 6
2
3 5
1 4
15
7
15
Hint
- Sample Explanation

As shown, the generator is attached to node and can connect to only one wire. The red line is the chosen wire. You can see that this is optimal.
- Constraints
For of the testdata: .
For of the testdata: .
For of the testdata: $n \le 30000, m \le 500000, q \le 30000, 1 \le s \le n, 1 \le k \le 150$.
For of the testdata: the wire lengths required to connect any two different pairs of nodes are all different (i.e. all edge weights are distinct), and it is guaranteed that no overflow will occur during computation.
- A Warm Reminder from the Problem Setter
The problem requires making the longest wire as short as possible; on this basis, make the second longest wire as short as possible, and so on.
Multiple edges are not ruled out, but the number of edges is sufficient, and no multiple edge will be chosen.
It is guaranteed that there are no self-loops, and the data is fully random.
Translated by ChatGPT 5
京公网安备 11011102002149号