529. It's Time to Repair the Roads
Time limit per test: 2.75
Memory limit: 262144
Everybody knows about the problems with roads in Berland. The government has been trying to undertake major repairs for many years, but the roads have never been repaired due to the lack of money in the budget.
There are n
cities and m
roads in Berland. The cities are numbered from 1 to n
. The roads are numbered from 1 to m
. Each road connects a pair of different cities, all the roads are two-way. There is at most one road between any pair of cities. The cost of repairing is known for each road.
Clearly, repairing all roads in Berland is an unaffordable luxury, so the government decided to repair only such set of the roads, that it's possible to get from any city to any other city by the roads from this repaired set, and the total cost of these road works is minimal.
In the circumstances of the global economic crisis and global warming, road repair costs change every day. Berland's scientists managed to predict these changes, concluding that the cost of road works will change for only one road each day. They created a full list of expected changes for the coming t
days — for each day they came up a road and its new repair cost.
The government of Berland would like to know when it would be better to repair the roads, so they need to figure out the cost of road works for every of the coming t
days before making a final decision. Your task is to help them and figure out the total repair cost of Berland's road system at the end of each these t
days. As repair costs change over time, the set of selected roads can change on a daily basis as well.
The first line contains a pair of integers n
(2 ≤ n
≤ 40000, n
- 1 ≤ m
≤ 40000), where n
— the amount of cities, m
— the amount of roads. Each of the following m
lines contains a road description: three integer numbers xi
(1 ≤ xi
, 1 ≤ pi
≤ 40000), where xi
are indices of the cities connected by the given road, and pi
— initial cost of repairing it.
Then there follows a line with the only number t
(1 ≤ t
≤ 40000), t
— amount of days. The following t
lines contain the scientists' predictions for the coming t
days. Each of t
lines contains a pair of integer numbers ei
(1 ≤ ei
, 1 ≤ ci
≤ 40000), where ci
— is the new repair cost for the road ei
It's possible to get from any city to any other city by the roads. The cost of repair for a single road can be changed more than once over time.
lines, each of them should contain the road system's total repair cost at the end of each day.
1 2 10
2 3 20
2 4 30
1 3 40
3 4 50
4 1 60
3 2 4
3 1 4
2 1 3