CF1468J.Road Reform
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
There are n cities and m bidirectional roads in Berland. The i-th road connects the cities xi and yi, and has the speed limit si. 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 m roads is too costly, so m−(n−1) roads will be demolished in such a way that the remaining (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 1, or decreasing it by 1. 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) roads with the maximum speed limit over all roads equal to exactly k. 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=7:

Then one of the optimal courses of action is to demolish the roads 1–4 and 3–4, and then decrease the speed limit on the road 2–3 by 1, so the resulting road network looks like that:

伯兰德有 n 座城市和 m 条双向道路。第 i 条道路连接城市 xi 和 yi,且限速为 si。该道路网络保证任意两座城市之间均可互相到达。
伯兰德交通部正计划开展一项道路改革。
首先,维护全部 m 条道路成本过高,因此将拆除其中 m−(n−1) 条道路,使得剩余的 (n−1) 条道路仍能保证任意两座城市之间均可互相到达。形式上,剩余道路应构成一棵无向树。
其次,剩余道路上的限速可能被调整。调整将按顺序进行,每次操作要么将某条道路的限速增加 1,要么减少 1。由于调整限速工作量巨大,交通部希望最小化调整次数。
交通部的目标是:最终保留的 (n−1) 条道路所构成的路网中,所有道路的最高限速恰好等于 k。他们委派你计算为达成此目标所需的最小限速调整次数。
例如,假设伯兰德初始地图如下所示,且 k=7:

则一种最优方案是:拆除道路 1–4 和 3–4,然后将道路 2–3 的限速降低 1,最终路网如下所示:

输入格式
The first line contains one integer t (1≤t≤1000) — the number of test cases.
The first line of each test case contains three integers n, m and k (2≤n≤2⋅105; n−1≤m≤min(2⋅105,2n(n−1)); 1≤k≤109) — the number of cities, the number of roads and the required maximum speed limit, respectively.
Then m lines follow. The i-th line contains three integers xi, yi and si (1≤xi,yi≤n; xi=yi; 1≤si≤109) — the cities connected by the i-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 n over all test cases does not exceed 2⋅105. Similarly, the sum of m over all test cases does not exceed 2⋅105.
第一行包含一个整数 t(1≤t≤1000)—— 测试用例的数量。
每个测试用例的第一行包含三个整数 n、m 和 k(2≤n≤2⋅105;n−1≤m≤min(2⋅105,2n(n−1));1≤k≤109)—— 分别表示城市的数量、道路的数量以及要求的最大限速。
接下来是 m 行。第 i 行包含三个整数 xi、yi 和 si(1≤xi,yi≤n;xi=yi;1≤si≤109)—— 分别表示第 i 条道路所连接的两个城市及其限速。所有道路均为双向道路。
每个测试用例中的道路网络是连通的(即任意两个城市之间均存在一条沿道路的路径),且每对城市之间至多由一条道路连接。
所有测试用例的 n 之和不超过 2⋅105。类似地,所有测试用例的 m 之和也不超过 2⋅105。
输出格式
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) roads is exactly k.
对于每个测试用例,输出一个整数——即交通部需要执行的最少修改次数,使得剩余 (n−1) 条道路中的最大限速值恰好为 k。
输入输出样例
输入#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 1–2, 3–2 and 3–4, and then increase the speed limit on the road 1–4 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 1–2 so the resulting road network meets the requirements.
示例测试的解释:
第一个测试用例已在题目描述中给出。
在第二个测试用例中,道路网络初始状态如下所示:

交通部可以拆除道路 1–2、3–2 和 3–4,然后将道路 1–4 的限速提升三次。
在第三个测试用例中,道路网络已满足全部要求。
在第四个测试用例中,仅需拆除道路 1–2,即可使最终的道路网络满足全部要求。
输入解题思路,AI测评打分。不知道怎么写?