CF1468J Road Reform 题解

2024-02-22 10:12
文章标签 题解 road reform cf1468j

本文主要是介绍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;
}

这篇关于CF1468J Road Reform 题解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/734928

相关文章

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

C - Word Ladder题解

C - Word Ladder 题解 解题思路: 先输入两个字符串S 和t 然后在S和T中寻找有多少个字符不同的个数(也就是需要变换多少次) 开始替换时: tips: 字符串下标以0开始 我们定义两个变量a和b,用于记录当前遍历到的字符 首先是判断:如果这时a已经==b了,那么就跳过,不用管; 如果a大于b的话:那么我们就让s中的第i项替换成b,接着就直接输出S就行了。 这样

【秋招笔试】9.07米哈游秋招改编题-三语言题解

🍭 大家好这里是 春秋招笔试突围,一起备战大厂笔试 💻 ACM金牌团队🏅️ | 多次AK大厂笔试 | 大厂实习经历 ✨ 本系列打算持续跟新 春秋招笔试题 👏 感谢大家的订阅➕ 和 喜欢💗 和 手里的小花花🌸 ✨ 笔试合集传送们 -> 🧷春秋招笔试合集 🍒 本专栏已收集 100+ 套笔试题,笔试真题 会在第一时间跟新 🍄 题面描述等均已改编,如果和你笔试题看到的题面描述

LeetCode 第414场周赛个人题解

目录 Q1. 将日期转换为二进制表示 原题链接 思路分析 AC代码 Q2. 范围内整数的最大得分 原题链接 思路分析 AC代码 Q3. 到达数组末尾的最大得分 原题链接 思路分析 AC代码 Q4. 吃掉所有兵需要的最多移动次数 原题链接 思路分析 AC代码 Q1. 将日期转换为二进制表示 原题链接 Q1. 将日期转换为二进制表示 思路分析

牛客小白月赛100部分题解

比赛地址:牛客小白月赛100_ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛_牛客竞赛OJ A.ACM中的A题 #include<bits/stdc++.h>using namespace std;#define ll long long#define ull = unsigned long longvoid solve() {ll a,b,c;cin>>a>>b>

P2858 [USACO06FEB] Treats for the Cows G/S 题解

P2858 题意 给一个数组。每天把最左或者最右的东西卖掉,第 i i i个东西,第 d a y day day天卖出的价格是 a [ i ] ∗ d a y a[i]*day a[i]∗day。 记忆化搜索 void dfs(int l,int r,int day,ll sum){if(v[l][r]>=sum)return;v[l][r]=sum;if(l>r)//这就是dp答案{

【C++题解】1272. 郭远摘苹果

欢迎关注本专栏《C++从零基础到信奥赛入门级(CSP-J)》 问题:1272. 郭远摘苹果 类型:二维数组 题目描述: 郭远有一天走到了一片苹果林,里面每颗树上都结有不同数目的苹果,郭远身上只能拿同一棵树上的苹果,他每到一棵果树前都会把自己身上的苹果扔掉并摘下他所在树上的苹果并带走(假设郭远会走过每一棵苹果树),问在郭远摘苹果的整个过程中,他身上携带的最多苹果数与最小苹果数的差是多少?

【最新华为OD机试E卷-支持在线评测】机器人活动区域(100分)多语言题解-(Python/C/JavaScript/Java/Cpp)

🍭 大家好这里是春秋招笔试突围 ,一枚热爱算法的程序员 ✨ 本系列打算持续跟新华为OD-E/D卷的三语言AC题解 💻 ACM金牌🏅️团队| 多次AK大厂笔试 | 编程一对一辅导 👏 感谢大家的订阅➕ 和 喜欢💗 🍿 最新华为OD机试D卷目录,全、新、准,题目覆盖率达 95% 以上,支持题目在线评测,专栏文章质量平均 94 分 最新华为OD机试目录: https://blog.

2023 CCPC(秦皇岛)现场(第二届环球杯.第 2 阶段:秦皇岛)部分题解

所有题目链接:Dashboard - The 2023 CCPC (Qinhuangdao) Onsite (The 2nd Universal Cup. Stage 9: Qinhuangdao) - Codeforces 中文题面: contest-37054-zh.pdf (codeforces.com) G. Path 链接: Problem - G - Codeforces