CF1566G.Four Vertices

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given an undirected weighted graph, consisting of nn vertices and mm 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 aa, bb, cc, dd such that there exists a path between aa and bb, there exists a path between cc and dd, and the sum of lengths of two shortest paths from aa to bb and from cc to dd 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.

给你一个包含 nn 个顶点和 mm 条边的无向带权图。

该图上会进行若干次查询,每次查询为以下两种操作之一:

  • 从图中删除一条已存在的边;
  • 向图中添加一条不存在的边。

在初始状态以及每次查询之后,你都需要找出四个互不相同的顶点 aa、bb、cc、dd,使得:

  • aa 与 bb 之间存在路径,
  • cc 与 dd 之间存在路径,
  • 且从 aa 到 bb 的最短路径长度与从 cc 到 dd 的最短路径长度之和最小。

该查询的答案即为这两个最短路径长度之和。路径的长度定义为该路径上所有边的权重之和。

输入格式

The first line contains two integers nn and mm (4≤n,m≤105)(4 \le n, m \le 10^5) — the number of vertices and edges in the graph respectively.

Each of the next mm lines contain three integers vv, uu, ww (1≤v,u≤n,v≠u1 \le v, u \le n, v \neq u, 1≤w≤1091 \le w \le 10^9) — this triple means that there is an edge between vertices vv and uu with weight ww.

The next line contains a single integer qq (0≤q≤105)(0 \le q \le 10^5) — the number of queries.

The next qq lines contain the queries of two types:

  • 00 vv uu — this query means deleting an edge between vv and uu (1≤v,u≤n,v≠u)(1 \le v, u \le n, v \neq u). It is guaranteed that such edge exists in the graph.
  • 11 vv uu ww — this query means adding an edge between vertices vv and uu with weight ww (1≤v,u≤n,v≠u1 \le v, u \le n, v \neq u, 1≤w≤1091 \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 44. It can be proven, that at each moment there exist some four vertices aa, bb, cc, dd such that there exists a path between vertices aa and bb, and there exists a path between vertices cc and dd.

第一行包含两个整数 nn 和 mm(4≤n,m≤1054 \le n, m \le 10^5),分别表示图中顶点和边的数量。

接下来的 mm 行,每行包含三个整数 vv、uu、ww(1≤v,u≤n, v≠u, 1≤w≤1091 \le v, u \le n,\ v \neq u,\ 1 \le w \le 10^9)——该三元组表示一条连接顶点 vv 和 uu、权重为 ww 的边。

下一行包含一个整数 qq(0≤q≤1050 \le q \le 10^5)——表示查询的数量。

接下来的 qq 行包含两类查询:

  • 00 vv uu —— 该查询表示删除顶点 vv 与 uu 之间的边(1≤v,u≤n, v≠u1 \le v, u \le n,\ v \neq u)。保证图中当前存在这样一条边。
  • 11 vv uu ww —— 该查询表示添加一条连接顶点 vv 和 uu、权重为 ww 的边(1≤v,u≤n, v≠u, 1≤w≤1091 \le v, u \le n,\ v \neq u,\ 1 \le w \le 10^9)。保证图中此前不存在这样一条边。

保证初始图中不含重边。

在初始状态以及每次查询之后,图均不强制要求连通。

保证在任意时刻,图中的边数至少为 44。可以证明:在任意时刻,总存在四个顶点 aa、bb、cc、dd,使得 aa 与 bb 之间存在路径,且 cc 与 dd 之间也存在路径。

输出格式

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

输出 q+1q + 1 个整数——即在所有查询之前,以及每次查询之后,所选顶点对之间最短路径长度之和的最小值。

输入输出样例

  • 输入#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)(a, b) = (3, 2) and (c,d)=(1,4)(c, d) = (1, 4). The sum of lengths of two shortest paths is 3+1=43 + 1 = 4.

After the first query you can choose vertices (a,b)=(2,5)(a, b) = (2, 5) and (c,d)=(1,4)(c, d) = (1, 4). The sum of lengths of two shortest paths is 2+1=32 + 1 = 3.

After the second query you can choose vertices (a,b)=(3,4)(a, b) = (3, 4) and (c,d)=(2,5)(c, d) = (2, 5). The sum of lengths of two shortest paths is 1+2=31 + 2 = 3.

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

After the last query you can choose vertices (a,b)=(1,6)(a, b) = (1, 6) and (c,d)=(2,5)(c, d) = (2, 5). The sum of lengths of two shortest paths is 3+2=53 + 2 = 5.

在查询之前,你可以选择顶点 (a,b)=(3,2)(a, b) = (3, 2) 和 (c,d)=(1,4)(c, d) = (1, 4)。两条最短路径的长度之和为 3+1=43 + 1 = 4。

第一次查询后,你可以选择顶点 (a,b)=(2,5)(a, b) = (2, 5) 和 (c,d)=(1,4)(c, d) = (1, 4)。两条最短路径的长度之和为 2+1=32 + 1 = 3。

第二次查询后,你可以选择顶点 (a,b)=(3,4)(a, b) = (3, 4) 和 (c,d)=(2,5)(c, d) = (2, 5)。两条最短路径的长度之和为 1+2=31 + 2 = 3。

第三次查询后,你可以选择顶点 (a,b)=(2,6)(a, b) = (2, 6) 和 (c,d)=(4,5)(c, d) = (4, 5)。两条最短路径的长度之和为 4+3=74 + 3 = 7。

最后一次查询后,你可以选择顶点 (a,b)=(1,6)(a, b) = (1, 6) 和 (c,d)=(2,5)(c, d) = (2, 5)。两条最短路径的长度之和为 3+2=53 + 2 = 5。

输入解题思路,AI测评打分。不知道怎么写?

首页