CF1468J Road Reform 题解

CF1468J Road Reform 题解

link

CF1468J Road Reform

题面翻译

给定一个有 n n n 个节点, m m m 条无向带权边的图,和一个参数 k k k,第 i i i 条边权值为 s i s_i si

现在你要保留这个图中的 n − 1 n-1 n1 条边使得这个图变成一棵树,然后你可以对这棵树上的任意边进行修改,每次修改可以使这个边的权值加上一或减去一。

现在你需要使所有边权的最大值正好等于 k k k,求所有保留方案的最小操作数。

T T T 组询问。

保证初始时给定的图满足任意两个点互相可达,没有重边或自环。

1 ≤ T ≤ 1 0 3 . 1\leq T\leq 10^3. 1T103.

1 ≤ n ≤ 2 × 1 0 5 ; n − 1 ≤ m ≤ min ⁡ ( n ( n + 1 ) 2 , 2 × 1 0 5 ) ; 1\leq n\leq2\times10^5;n-1\leq m\leq \min(\frac{n(n+1)}{2},2\times10^5); 1n2×105;n1mmin(2n(n+1),2×105);

∑ n , ∑ m ≤ 2 × 1 0 5 ; \sum n,\sum m\leq2\times10^5; n,m2×105;

1 ≤ k , s i ≤ 1 0 9 . 1\leq k,s_i\leq 10^9. 1k,si109.

题目描述

There are n n n cities and m m m bidirectional roads in Berland. The i i i -th road connects the cities x i x_i xi and y i y_i yi , and has the speed limit s i s_i 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 m m roads is too costly, so m − ( n − 1 ) m - (n - 1) m(n1) roads will be demolished in such a way that the remaining ( n − 1 ) (n - 1) (n1) 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 1 1 , or decreasing it by 1 1 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 ) (n - 1) (n1) roads with the maximum speed limit over all roads equal to exactly k k 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 k = 7 k=7 :

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

输入格式

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

The first line of each test case contains three integers n n n , m m m and k k k ( 2 ≤ n ≤ 2 ⋅ 1 0 5 2 \le n \le 2 \cdot 10^5 2n2105 ; n − 1 ≤ m ≤ min ⁡ ( 2 ⋅ 1 0 5 , n ( n − 1 ) 2 ) n - 1 \le m \le \min(2 \cdot 10^5, \frac{n(n-1)}{2}) n1mmin(2105,2n(n1)) ; 1 ≤ k ≤ 1 0 9 1 \le k \le 10^9 1k109 ) — 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 x i x_i xi , y i y_i yi and s i s_i si ( 1 ≤ x i , y i ≤ n 1 \le x_i, y_i \le n 1xi,yin ; x i ≠ y i x_i \ne y_i xi=yi ; 1 ≤ s i ≤ 1 0 9 1 \le s_i \le 10^9 1si109 ) — the cities connected by the i i 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 ⋅ 1 0 5 2 \cdot 10^5 2105 . Similarly, the sum of m m m over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105 .

输出格式

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) (n1) roads is exactly k k k .

样例 #1

样例输入 #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 1 1 2 2 2 , 3 3 3 2 2 2 and 3 3 3 4 4 4 , and then increase the speed limit on the road 1 1 1 4 4 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 1 1 2 2 2 so the resulting road network meets the requirements.

算法:最小生成树

思路:
首先看题。

题目中说:要在图中保留 n − 1 n−1 n1 条边,使它变成一棵树。

因此想到 最小生成树。

我这里用的是 Kruskal,需要用到并查集。

因此要注意并查集要初始化!

大家应该都知道 Kruskal 算法的流程是:先对边权从小到大排序,再枚举每一个 i i i,看一下所对应的 u u u v v v 是否在同一个集合内。如果不是,就可以选择这一条边。

做完最小生成树以后,我们要进行分类讨论:

计最小生成树中最大的边权为 t t t

t < k t<k t<k 时,遍历所有边,取与 k k k 差值最小的即可。

t > k t>k t>k 时,将所有边权与 k k k 的差值相加即可。

注意多测要清空即可。

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll N=4e5+10,inf=2e9;
ll T,n,m,k,t,ans,ct,fa[N];
struct E{ll u,v,w;
}a[N];
bool cmp(E l,E r){return l.w<r.w;
}
void jian(){for(int i=1;i<=n;i++) fa[i]=i;
}
ll getfa(ll x){return fa[x]==x?x:fa[x]=getfa(fa[x]);
}
void kruskal(){jian();ct=t=ans=0;//多测不清空,爆零两行泪!sort(a+1,a+m+1,cmp);for(int i=1;i<=m;i++){ll x=getfa(a[i].u),y=getfa(a[i].v);if(x==y) continue;t=a[i].w,ans+=max(t-k,0ll),fa[x]=y;//因为边权是从小到大枚举的,所以当前值一定是最大的}
}
int main(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cin>>T;while(T--){cin>>n>>m>>k;for(int i=1;i<=m;i++)cin>>a[i].u>>a[i].v>>a[i].w;kruskal();if(t<k){ans=inf;//不要忘记for(int i=1;i<=m;i++)ans=min(ans,abs(a[i].w-k));}//t>k的情况在做最小生成树的时候顺便求出来了cout<<ans<<"\n";}return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/694221.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java导出动态下拉框excel模板

1.原始模板 2.导出模板,下拉框为数据库中得到动态数据 public void downloadTemplate(HttpServletResponse response) throws IOException {// 所有部门List<String, String> departments expertManageMapper.selectAllDepartment();//所有职位List<String, String&g…

前端关于Vue跳转外部链接(百度为例)

一定要加这句 window.location.href http://www.baidu.com/s?wd this.wd;关于Vue跳转外部链接&#xff08;百度为例&#xff09;_vue3.2 点击按钮 router.resolve 跳转 百度-CSDN博客 <img src"./hd1.jpg" width"200px" height"100px" c…

模块 time:时间和日期处理

MicroPython内置模块 time&#xff1a;时间和日期处理 MicroPython的内置模块time是一个用于处理时间相关功能的模块&#xff0c;它实现了CPython模块的一个子集&#xff0c;但也有一些特殊的特点和限制。本文将从以下几个方面介绍time模块的主要特点、应用场景&#xff0c;以…

基于Java (spring-boot)的社区物业管理系统

一、项目介绍 本系统共分为两个角色&#xff1a;管理员和业主。 主要功能有&#xff0c;核心业务处理&#xff0c;基础信息管理&#xff0c;数据统计分析 核心业务处理&#xff1a;车位收费管理&#xff0c;物业收费管理&#xff0c;投诉信息管理&#xff0c;保修信息管理。 …

Top-N 泛型工具类

一、代码实现 public class TopNUtil<E extends Comparable<E>> {private final PriorityQueue<E> priorityQueue;private final int n;/*** 构造 Top-N*/public TopNUtil(int size) {if (size < 0) {throw new IllegalArgumentException("Top-N si…

嵌入式学习day22 Linux

文件IO: 1. lseek off_t lseek(int fd, off_t offset, int whence); 功能: 重新设定文件描述符的偏移量 参数: fd:文件描述符 offset:偏移量 whence: SEEK_SET 文件开头 …

代码随想录三刷day04

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、力扣209. 长度最小的子数组二、力扣904. 水果成篮三、力扣76. 最小覆盖子串 前言 接下来就开始介绍数组操作中另一个重要的方法&#xff1a;滑动窗口。 所谓…

C++从入门到精通 第十四章(STL容器)【下】

七、list容器 1、list的基本概念 &#xff08;1&#xff09;list的功能是将数据进行链式存储&#xff0c;对应数据结构中的链表&#xff0c;链表是一种物理存储单元上非连续的存储结构&#xff0c;数据元素的逻辑顺序是通过链表中的指针链接实现的。 &#xff08;2&#xff…

《VitePress 简易速速上手小册》第2章:Markdown 与页面创建(2024 最新版)

文章目录 2.1 Markdown 基础及扩展2.1.1 基础知识点解析2.1.2 重点案例&#xff1a;技术博客2.1.3 拓展案例 1&#xff1a;食谱分享2.1.4 拓展案例 2&#xff1a;个人旅行日记 2.2 页面结构与布局设计2.2.1 基础知识点解析2.2.2 重点案例&#xff1a;公司官网2.2.3 拓展案例 1&…

Linux系统运维:性能监视和分析工具sar命令详解

目 录 一、sar工具介绍 二、sar工作原理 &#xff08;一&#xff09;原理概述 &#xff08;二&#xff09;sar数据收集器 三、sar命令语法 四、sar主要功能介绍 &#xff08;一&#xff09;功能概述 &#xff08;二&#xff09;CPU统计数据 &#xff08;三&a…

jwt+redis实现登录认证

项目环境&#xff1a;spring boot项目 pom.xml引入jwt和redis <!-- jwt --><dependency><groupId>com.auth0</groupId><artifactId>java-jwt</artifactId><version>4.3.0</version></dependency><!-- redis坐标-->…

数据结构与算法:队列

在上篇文章讲解了栈之后&#xff0c;本篇也对这一章进行收尾&#xff0c;来到队列&#xff01; 队列 队列的介绍队列的存储结构队列顺序存储的不足之处 循环队列的定义队列的链式存储结构链队列的构建链队列的初始化队尾入队队头出队获取队头队尾元素判断队列是否为空获取队列元…

Spark解决代码变量bug:error: reassingnment to val

在Scala中&#xff0c;val关键字用于声明一个不可变的变量&#xff0c;一旦赋值后就不能再更改。这就是为什么我尝试重新赋值给modelFilePath时会收到“reassignment to val”的错误。 modelFilePath的声明从val更改为var&#xff0c;因为var允许我们重新赋值 样例代码 val …

267.【华为OD机试真题】贪心歌手(贪心策略-JavaPythonC++JS实现)

🚀点击这里可直接跳转到本专栏,可查阅顶置最新的华为OD机试宝典~ 本专栏所有题目均包含优质解题思路,高质量解题代码(Java&Python&C++&JS分别实现),详细代码讲解,助你深入学习,深度掌握! 文章目录 一. 题目-贪心歌手二.解题思路三.题解代码Python题解代码…

SQL常用函数收藏

日期时间相关 【date_format】 DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据。 参考地址&#xff1a;https://www.runoob.com/sql/func-date-format.html -- 函数&#xff1a;date_format select DATE_FORMAT(Now(),"%Y-%m-%d %H:%i:%S") as v_current_da…

【Java前端技术栈】模块化编程

一、基本介绍 1.基本介绍 1 传统非模块化开发有如下的缺点&#xff1a;(1)命名冲突 (2)文件依赖 2 Javascript 代码越来越庞大&#xff0c;Javascript 引入模块化编程&#xff0c;开发者只需要实现核心的业务逻辑&#xff0c;其他都可以加载别人已经写好的模块 3 Javascrip…

torch.utils.data

整体架构 平时使用 pytorch 加载数据时大概是这样的&#xff1a; import numpy as np from torch.utils.data import Dataset, DataLoaderclass ExampleDataset(Dataset):def __init__(self):self.data [1, 2, 3, 4, 5]def __getitem__(self, idx):return self.data[idx]def…

第2.2章 StarRocks表设计——排序键和数据模型

该篇文章介绍StarRocks-2.5.4版本的数据模型相关内容&#xff0c;有误请指出~ 目录 一、数据模型概述 1.1 四种模型 1.2 排序键 1.2.1 概述 1.2.2 分类 1.2.3 注意事项 二、明细模型 2.1 概述 2.2 适用场景 2.3 建表语句及说明 三、聚合模型 3.1 概述 3.2 适用场…

网络入门基础

本专栏内容为&#xff1a;Linux学习专栏&#xff0c;分为系统和网络两部分。 通过本专栏的深入学习&#xff0c;你可以了解并掌握Linux。 &#x1f493;博主csdn个人主页&#xff1a;小小unicorn ⏩专栏分类&#xff1a;网络 &#x1f69a;代码仓库&#xff1a;小小unicorn的代…

[AIGC] JVM内存结构中的方法区主要存储哪些信息?

在JVM的内存结构中&#xff0c;方法区&#xff08;Method Area&#xff09;被视为JVM的永久代。它主要负责存储已经被虚拟机加载的类信息、常量、静态变量以及编译器编译后的代码等数据。具体可以分为以下几个部分&#xff1a; 类信息 这部分信息包括类数据&#xff08;如类名…