CF1468J.Road Reform

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

There are nn cities and mm bidirectional roads in Berland. The ii-th road connects the cities xix_i and yiy_i, and has the speed limit sis_i. The road network allows everyone to get from any city to any other city.

The Berland Transport Ministry is planning a road reform.

First of all, maintaining all mm roads is too costly, so m−(n−1)m - (n - 1) roads will be demolished in such a way that the remaining (n−1)(n - 1) roads still allow to get to any city from any other city. Formally, the remaining roads should represent an undirected tree.

Secondly, the speed limits on the remaining roads might be changed. The changes will be done sequentially, each change is either increasing the speed limit on some road by 11, or decreasing it by 11. Since changing the speed limit requires a lot of work, the Ministry wants to minimize the number of changes.

The goal of the Ministry is to have a road network of (n−1)(n - 1) roads with the maximum speed limit over all roads equal to exactly kk. They assigned you the task of calculating the minimum number of speed limit changes they have to perform so the road network meets their requirements.

For example, suppose the initial map of Berland looks like that, and k=7k = 7:

Then one of the optimal courses of action is to demolish the roads 11–44 and 33–44, and then decrease the speed limit on the road 22–33 by 11, so the resulting road network looks like that:

伯兰德有 nn 座城市和 mm 条双向道路。第 ii 条道路连接城市 xix_i 和 yiy_i,且限速为 sis_i。该道路网络保证任意两座城市之间均可互相到达。

伯兰德交通部正计划开展一项道路改革。

首先,维护全部 mm 条道路成本过高,因此将拆除其中 m−(n−1)m - (n - 1) 条道路,使得剩余的 (n−1)(n - 1) 条道路仍能保证任意两座城市之间均可互相到达。形式上,剩余道路应构成一棵无向树。

其次,剩余道路上的限速可能被调整。调整将按顺序进行,每次操作要么将某条道路的限速增加 11,要么减少 11。由于调整限速工作量巨大,交通部希望最小化调整次数。

交通部的目标是:最终保留的 (n−1)(n - 1) 条道路所构成的路网中,所有道路的最高限速恰好等于 kk。他们委派你计算为达成此目标所需的最小限速调整次数。

例如,假设伯兰德初始地图如下所示,且 k=7k = 7:

则一种最优方案是:拆除道路 11–44 和 33–44,然后将道路 22–33 的限速降低 11,最终路网如下所示:

输入格式

The first line contains one integer tt (1≤t≤10001 \le t \le 1000) — the number of test cases.

The first line of each test case contains three integers nn, mm and kk (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5; n−1≤m≤min⁡(2⋅105,n(n−1)2)n - 1 \le m \le \min(2 \cdot 10^5, \frac{n(n-1)}{2}); 1≤k≤1091 \le k \le 10^9) — the number of cities, the number of roads and the required maximum speed limit, respectively.

Then mm lines follow. The ii-th line contains three integers xix_i, yiy_i and sis_i (1≤xi,yi≤n1 \le x_i, y_i \le n; xi≠yix_i \ne y_i; 1≤si≤1091 \le s_i \le 10^9) — the cities connected by the ii-th road and the speed limit on it, respectively. All roads are bidirectional.

The road network in each test case is connected (that is, it is possible to reach any city from any other city by traveling along the road), and each pair of cities is connected by at most one road.

The sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5. Similarly, the sum of mm over all test cases does not exceed 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤10001 \le t \le 1000)—— 测试用例的数量。

每个测试用例的第一行包含三个整数 nn、mm 和 kk(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5;n−1≤m≤min⁡(2⋅105,n(n−1)2)n - 1 \le m \le \min(2 \cdot 10^5, \frac{n(n-1)}{2});1≤k≤1091 \le k \le 10^9)—— 分别表示城市的数量、道路的数量以及要求的最大限速。

接下来是 mm 行。第 ii 行包含三个整数 xix_i、yiy_i 和 sis_i(1≤xi,yi≤n1 \le x_i, y_i \le n;xi≠yix_i \ne y_i;1≤si≤1091 \le s_i \le 10^9)—— 分别表示第 ii 条道路所连接的两个城市及其限速。所有道路均为双向道路。

每个测试用例中的道路网络是连通的(即任意两个城市之间均存在一条沿道路的路径),且每对城市之间至多由一条道路连接。

所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。类似地,所有测试用例的 mm 之和也不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, print one integer — the minimum number of changes the Ministry has to perform so that the maximum speed limit among the remaining (n−1)(n - 1) roads is exactly kk.

对于每个测试用例,输出一个整数——即交通部需要执行的最少修改次数,使得剩余 (n−1)(n - 1) 条道路中的最大限速值恰好为 kk。

输入输出样例

  • 输入#1

    4
    4 5 7
    4 1 3
    1 2 5
    2 3 8
    2 4 1
    3 4 4
    4 6 5
    1 2 1
    1 3 1
    1 4 2
    2 4 1
    4 3 1
    3 2 1
    3 2 10
    1 2 8
    1 3 10
    5 5 15
    1 2 17
    3 1 15
    2 3 10
    1 4 14
    2 5 8

    输出#1

    1
    3
    0
    0

说明/提示

The explanation for the example test:

The first test case is described in the problem statement.

In the second test case, the road network initially looks like that:

The Ministry can demolish the roads 11–22, 33–22 and 33–44, and then increase the speed limit on the road 11–44 three times.

In the third test case, the road network already meets all the requirements.

In the fourth test case, it is enough to demolish the road 11–22 so the resulting road network meets the requirements.

示例测试的解释:

第一个测试用例已在题目描述中给出。

在第二个测试用例中,道路网络初始状态如下所示:

交通部可以拆除道路 11–22、33–22 和 33–44,然后将道路 11–44 的限速提升三次。

在第三个测试用例中,道路网络已满足全部要求。

在第四个测试用例中,仅需拆除道路 11–22,即可使最终的道路网络满足全部要求。

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

首页