codeforces#P1566G. Four Vertices
Four Vertices
Description
You are given an undirected weighted graph, consisting of vertices and edges.
Some queries happen with this graph:
- Delete an existing edge from the graph.
- Add a non-existing edge to the graph.
At the beginning and after each query, you should find four different vertices , , , such that there exists a path between and , there exists a path between and , and the sum of lengths of two shortest paths from to and from to is minimal. The answer to the query is the sum of the lengths of these two shortest paths. The length of the path is equal to the sum of weights of edges in this path.
The first line contains two integers and — the number of vertices and edges in the graph respectively.
Each of the next lines contain three integers , , (, ) — this triple means that there is an edge between vertices and with weight .
The next line contains a single integer — the number of queries.
The next lines contain the queries of two types:
- — this query means deleting an edge between and . It is guaranteed that such edge exists in the graph.
- — this query means adding an edge between vertices and with weight (, ). It is guaranteed that there was no such edge in the graph.
It is guaranteed that the initial graph does not contain multiple edges.
At the beginning and after each query, the graph doesn't need to be connected.
It is guaranteed that at each moment the number of edges will be at least . It can be proven, that at each moment there exist some four vertices , , , such that there exists a path between vertices and , and there exists a path between vertices and .
Print integers — the minimal sum of lengths of shortest paths between chosen pairs of vertices before the queries and after each of them.
Input
The first line contains two integers and — the number of vertices and edges in the graph respectively.
Each of the next lines contain three integers , , (, ) — this triple means that there is an edge between vertices and with weight .
The next line contains a single integer — the number of queries.
The next lines contain the queries of two types:
- — this query means deleting an edge between and . It is guaranteed that such edge exists in the graph.
- — this query means adding an edge between vertices and with weight (, ). It is guaranteed that there was no such edge in the graph.
It is guaranteed that the initial graph does not contain multiple edges.
At the beginning and after each query, the graph doesn't need to be connected.
It is guaranteed that at each moment the number of edges will be at least . It can be proven, that at each moment there exist some four vertices , , , such that there exists a path between vertices and , and there exists a path between vertices and .
Output
Print integers — the minimal sum of lengths of shortest paths between chosen pairs of vertices before the queries and after each of them.
Samples
Note
Before the queries you can choose vertices and . The sum of lengths of two shortest paths is .
After the first query you can choose vertices and . The sum of lengths of two shortest paths is .
After the second query you can choose vertices and . The sum of lengths of two shortest paths is .
After the third query, you can choose vertices and . The sum of lengths of two shortest paths is .
After the last query you can choose vertices and . The sum of lengths of two shortest paths is .