欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1468J.Road Reform
Road Reform
There are cities and bidirectional roads in Berland. The -th road connects the cities and , and has the speed limit . 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 roads is too costly, so roads will be demolished in such a way that the remaining 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 , or decreasing it by . 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 roads with the maximum speed limit over all roads equal to exactly . 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 :

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

Input
The first line contains one integer () — the number of test cases.
The first line of each test case contains three integers , and (; $n - 1 \le m \le \min(2 \cdot 10^5, \frac{n(n-1)}{2})$; ) — the number of cities, the number of roads and the required maximum speed limit, respectively.
Then lines follow. The -th line contains three integers , and (; ; ) — the cities connected by the -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 over all test cases does not exceed . Similarly, the sum of over all test cases does not exceed .
Output
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 roads is exactly .
Note
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 –, – and –, and then increase the speed limit on the road – 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 – so the resulting road network meets the requirements.
Samples
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
3
0
0
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |