308734: CF1566G. Four Vertices

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Four Vertices

题意翻译

### 题意 给定一张 $n$ 个点 $m$ 条边的带权无向图,现有 $q$ 次如下两种操作: - `0 u v`:删除边 $\langle u,v \rangle$,保证 $\langle u,v \rangle$ 存在; - `1 u v w`:添加边 $\langle u, v \rangle$,边权为 $w$,保证加边前 $\langle u, v \rangle$ 不存在。 初始时和每次操作后,需要找出 $4$ 个不同的点 $a, b, c, d$,满足 $a,b$ 联通,$c,d$ 联通,且 $a$ 到 $b$ 的最短路径与 $c$ 到 $d$ 的最短路径权值之和最小,求出这个最小权值和。这里最短路径即指边权和最小的路径。 数据保证初始时无重边,保证任意时刻图中边数不少于 $4$;不保证图联通。 ### 数据范围 - $4 \leq n,m \leq 10^5$ - $1 \leq w \leq 10^9$ - $0 \leq q \leq 10^5$

题目描述

You are given an undirected weighted graph, consisting of $ n $ vertices and $ m $ 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 $ a $ , $ b $ , $ c $ , $ d $ such that there exists a path between $ a $ and $ b $ , there exists a path between $ c $ and $ d $ , and the sum of lengths of two shortest paths from $ a $ to $ b $ and from $ c $ to $ d $ 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 $ n $ and $ m $ $ (4 \le n, m \le 10^5) $ — the number of vertices and edges in the graph respectively. Each of the next $ m $ lines contain three integers $ v $ , $ u $ , $ w $ ( $ 1 \le v, u \le n, v \neq u $ , $ 1 \le w \le 10^9 $ ) — this triple means that there is an edge between vertices $ v $ and $ u $ with weight $ w $ . The next line contains a single integer $ q $ $ (0 \le q \le 10^5) $ — the number of queries. The next $ q $ lines contain the queries of two types: - $ 0 $ $ v $ $ u $ — this query means deleting an edge between $ v $ and $ u $ $ (1 \le v, u \le n, v \neq u) $ . It is guaranteed that such edge exists in the graph. - $ 1 $ $ v $ $ u $ $ w $ — this query means adding an edge between vertices $ v $ and $ u $ with weight $ w $ ( $ 1 \le v, u \le n, v \neq u $ , $ 1 \le w \le 10^9 $ ). 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 $ 4 $ . It can be proven, that at each moment there exist some four vertices $ a $ , $ b $ , $ c $ , $ d $ such that there exists a path between vertices $ a $ and $ b $ , and there exists a path between vertices $ c $ and $ d $ .

输出格式


Print $ q + 1 $ integers — the minimal sum of lengths of shortest paths between chosen pairs of vertices before the queries and after each of them.

输入输出样例

输入样例 #1

6 6
1 3 6
4 3 1
1 4 1
2 6 4
2 4 2
5 4 3
4
1 2 5 2
0 1 4
0 3 4
1 6 1 3

输出样例 #1

4
3
3
7
5

说明

Before the queries you can choose vertices $ (a, b) = (3, 2) $ and $ (c, d) = (1, 4) $ . The sum of lengths of two shortest paths is $ 3 + 1 = 4 $ . After the first query you can choose vertices $ (a, b) = (2, 5) $ and $ (c, d) = (1, 4) $ . The sum of lengths of two shortest paths is $ 2 + 1 = 3 $ . After the second query you can choose vertices $ (a, b) = (3, 4) $ and $ (c, d) = (2, 5) $ . The sum of lengths of two shortest paths is $ 1 + 2 = 3 $ . After the third query, you can choose vertices $ (a, b) = (2, 6) $ and $ (c, d) = (4, 5) $ . The sum of lengths of two shortest paths is $ 4 + 3 = 7 $ . After the last query you can choose vertices $ (a, b) = (1, 6) $ and $ (c, d) = (2, 5) $ . The sum of lengths of two shortest paths is $ 3 + 2 = 5 $ .

Input

题意翻译

### 题意 给定一张 $n$ 个点 $m$ 条边的带权无向图,现有 $q$ 次如下两种操作: - `0 u v`:删除边 $\langle u,v \rangle$,保证 $\langle u,v \rangle$ 存在; - `1 u v w`:添加边 $\langle u, v \rangle$,边权为 $w$,保证加边前 $\langle u, v \rangle$ 不存在。 初始时和每次操作后,需要找出 $4$ 个不同的点 $a, b, c, d$,满足 $a,b$ 联通,$c,d$ 联通,且 $a$ 到 $b$ 的最短路径与 $c$ 到 $d$ 的最短路径权值之和最小,求出这个最小权值和。这里最短路径即指边权和最小的路径。 数据保证初始时无重边,保证任意时刻图中边数不少于 $4$;不保证图联通。 ### 数据范围 - $4 \leq n,m \leq 10^5$ - $1 \leq w \leq 10^9$ - $0 \leq q \leq 10^5$

加入题单

上一题 下一题 算法标签: