codeforce 378 div 2 F —— Drivers Dissatisfaction (最小生成树,LCA,倍增)
官方题解:
If you choose any n - 1 roads then price of reducing overall dissatisfaction is equal to min(c1, c2, ..cn - 1) where сi is price of reducing by 1 dissatisfaction of i-th edge. So the best solution is to choose one edge and reduce dissatisfaction of it until running out of budget.
Let's construct minimal spanning tree using Prim or Kruskal algorithm using edges of weights equal to dissatisfaction and calculate minimal price of reducing dissatisfaction. Time complexity — .
Now we can iterate over edges implying that current is the one to be reduced to minimum. For example, for every edge we can build new MST and recalculate answer. It's . Therefore we should use this fact: it's poinless to reduce dissatisfaction of edges which weren't selected to be main.
Then we can transform original MST instead of constructing m new ones. Add next edge to MST, now it contains a cycle from which edge with maximal dissatisfaction is about to be deleted. This can be achieved in such a way: find LCA of vertices of new edge in and using binary lifting with precalc in
find the edge to delete.
Time complexity — .
大意是:
经分析所有的预算用在一条干道上最合算(这条干道的修路花费是所挑选的n-1条干道中最小的),首先建立一棵最小生成树MST,然后枚举M条边,假设当前枚举到的边编号是i,将所有预算用到边 i 上然后添加到MST中形成一个环,找到环中权值最大的边删除。设边i的两端点为 a, b, 找到结点u = LCA(MST,a, b),那么这个环就是由 a~u,b~u 以及边 i 围成。
如何找到权值最大的边呢?可以通过倍增法,具体做法如下:
设mw[a][j]表示结点 a 到它的第 2^j 倍祖先的路径上权值最大的边编号,类似LCA倍增法的做法预处理出所有结点的mw[a][j]值
for(int j = ; (<<j) < n; j++) //倍增
for(int i = ; i <= n; i++) if(pa[i][j-] != -){
pa[i][j] = pa[pa[i][j-]][j-]; //计算节点i的第2^j倍祖先
int e1 = mw[i][j-];
int e2 = mw[pa[i][j-]][j-];
mw[i][j] = weight[e1] < weight[e2] ? e2 : e1;
}
然后计算更换边之后新树的权值,找到使总干道权值最低的更新方法,最后跑一边最小生成树算法即可。
代码如下:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue> using namespace std;
const int maxn = 2e5 + ; #define rep(i, x, n) for(int i = x; i <= n; i++)
struct edge{
int u, v;
int w, c;
edge(int uu, int vv, int ww, int cc): u(uu), v(vv), w(ww), c(cc){}
bool operator<(const edge& b) const {
return w < b.w;
}
}; typedef long long LL;
vector<edge> E;
int r[maxn];
vector<int> G[maxn]; void add_edge(int a, int b, int w, int c) {
E.push_back(edge(a, b, w, c));
int id = E.size() - ;
G[a].push_back(id);
G[b].push_back(id);
} int n, m, S;
int w[maxn];
int c[maxn];
int fa[maxn];
LL w_mst;
int findfa(int x) {return x == fa[x] ? x : fa[x] = findfa(fa[x]);} //并查集
vector<int> G2[maxn];
bool cmp(int a, int b) {
return E[a] < E[b];
}
vector<int> A;
LL kurskal() { //计算MST
LL ans = ;
for(int i = ; i <= n; i++) fa[i] = i;
for(int i = ; i < m; i++) r[i] = i;
sort(r, r+m, cmp);
for(int i = ; i < m; i++) {
edge e = E[r[i]];
int x = findfa(e.u);
int y = findfa(e.v);
if(x != y) {
fa[x] = y;
ans += e.w;
A.push_back(r[i]); //将添加的边同时保存在A中
G2[e.u].push_back(r[i]);
G2[e.v].push_back(r[i]);
}
}
return ans;
}
int dep[maxn];
int pa[maxn][]; //表示结点a的第 2^j 倍祖先
int mw[maxn][]; //表示结点 a 到它的第 2^j 倍祖先的路径上权值最大的边编号
void dfs(int u, int f, int d) { //dfs计算MST中所有结点的深度,初始化pa数组和mw数组
dep[u] = d;
pa[u][] = f;
for(int i = ; i < G2[u].size(); i++) {
edge& e = E[G2[u][i]];
int v = e.u == u ? e.v : e.u;
if(v != f) {
mw[v][] = G2[u][i];
dfs(v, u, d+);
}
}
}
void pre() { //预处理出所有结点的mw,pa
for(int j = ; (<<j) < n; j++)
for(int i = ; i <= n; i++) if(pa[i][j-] != -){
pa[i][j] = pa[pa[i][j-]][j-];
int e1 = mw[i][j-];
int e2 = mw[pa[i][j-]][j-];
mw[i][j] = E[e1] < E[e2] ? e2 : e1;
}
} int lca(int a, int b, int &me) { //计算最近公共祖先的同时算出a,b路径中权值最大的边保存到me中
me = -;
if(dep[a] < dep[b]) swap(a, b);
int i, j;
for(i = ; (<<i) <= dep[a]; i++);
i--;
for(j = i; j >= ; j--)
if(dep[a] - (<<j) >= dep[b]) {
int e_id = mw[a][j];
a = pa[a][j];
if(me == -) me = e_id;
me = E[me] < E[e_id] ? e_id : me;
}
if(a == b) return a;
for(j = i; j >= ; j--)
if(pa[a][j] != - && pa[a][j] != pa[b][j]) {
int e1 = mw[a][j];
int e2 = mw[b][j];
a = pa[a][j];
b = pa[b][j];
if(me == -) me = e1;
me = E[me] < E[e1] ? e1 : me;
me = E[me] < E[e2] ? e2 : me;
}
int e1 = mw[a][];
int e2 = mw[b][];
if(me == -) me = e1;
me = E[me] < E[e1] ? e1 : me;
me = E[me] < E[e2] ? e2 : me;
return pa[a][];
}
int main() {
scanf("%d%d", &n, &m);
for(int i = ; i <= m; i++) scanf("%d", &w[i]);
for(int i = ; i <= m; i++) scanf("%d", &c[i]);
for(int i = ; i <= m; i++) {
int a, b;
scanf("%d%d", &a, &b);
add_edge(a, b, w[i], c[i]);
}
scanf("%d", &S);
w_mst = kurskal();
memset(pa, -, sizeof pa);
memset(mw, , sizeof mw);
dfs(, -, );
pre();
LL ans_w = w_mst;
int ans_e = -;
for(int i = ; i <= m; i++) {
int e = i-;
int me;
lca(E[e].u, E[e].v, me);
LL temp = w_mst - E[me].w + E[e].w - S/c[i];
//cout << i <<" : me : "<< me <<endl;
if(temp < ans_w) {
ans_w = temp;
ans_e = e;
//cout << "ans_w : " << ans_w<< "ans_e : " << ans_e+1 <<endl;
}
}
if(ans_e != -) E[ans_e].w -= S/c[ans_e+];
A.clear();
w_mst = kurskal();
cout << ans_w << endl;
for(int i = ; i < A.size(); i++)
cout << A[i] + << " " << E[A[i]].w << endl;
return ;
}
codeforce 378 div 2 F —— Drivers Dissatisfaction (最小生成树,LCA,倍增)的更多相关文章
- Codeforces Round #378 (Div. 2) F - Drivers Dissatisfaction
F - Drivers Dissatisfaction 题目大意:给你n个点,m条边,每个边都有一个权重w,每条边也有一个c表示,消耗c元可以把这条边的权重减1,求最多消耗s元的最小生成树. 思路:因 ...
- Codeforces Round #378 (Div. 2)F - Drivers Dissatisfaction GNU
http://codeforces.com/contest/733/problem/F 题意:给你一些城市和一些路,每条路有不满意程度和每减少一点不满意程度的花费,给出最大花费,要求找出花费小于s的最 ...
- Drivers Dissatisfaction 最小生成树+LCA
题意:给一张n个点m条边的连通图,每条边(ai,bi)有一个权值wi和费用ci, 表示这条边每降低1的权值需要ci的花费.现在一共有S费用可以用来降低某些边的权值 (可以降到负数),求图中的一棵权值和 ...
- Codeforces Round #378 (Div. 2)F
题目:一个带权连通无向图,给第i条边权值减1需要花费ci元,你一共有S元,求最小生成树. 容易得出钱全部花在一条边上是最优的. 我们先做一遍最小生成树. 然后我们枚举减哪一条边. 如果这条边是树上的, ...
- CF733F Drivers Dissatisfaction【链剖】【最小生成树应用】
F. Drivers Dissatisfaction time limit per test 4 seconds memory limit per test 256 megabytes input s ...
- Drivers Dissatisfaction
Drivers Dissatisfaction time limit per test 4 seconds memory limit per test 256 megabytes input stan ...
- Codeforces Round #378 (Div. 2)
A: 思路: 水题,没啥意思; B: 思路: 暴力,也没啥意思; C: 思路: 思维,可以发现从前往后和为b[i]的分成一块,然后这一块里面如果都相同就没法开始吃,然后再暴力找到那个最大的且能一开始就 ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
随机推荐
- 二叉树总结—建树和4种遍历方式(递归&&非递归)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013497151/article/details/27967155 今天总结一下二叉树.要考离散了 ...
- Leetcode874.Walking Robot Simulation模拟行走的机器人
机器人在一个无限大小的网格上行走,从点 (0, 0) 处开始出发,面向北方.该机器人可以接收以下三种类型的命令: -2:向左转 90 度 -1:向右转 90 度 1 <= x <= 9:向 ...
- Liferay 7 OSGi第三方jar包依赖问题
发现遇到这个问题的人挺多的,现在跟新一下好好写. 在开发Liferay的时候,我们常常会遇到Unresolved requirement: Import-Package: {package name} ...
- 深入浅出Cocoa之消息【转】
在入门级别的ObjC 教程中,我们常对从C++或Java 或其他面向对象语言转过来的程序员说,ObjC 中的方法调用(ObjC中的术语为消息)跟其他语言中的方法调用差不多,只是形式有些不同而已. 譬如 ...
- 深入理解 Node.js 进程与线程
原文链接: https://mp.weixin.qq.com/s?__biz=MzAxODE2MjM1MA==&mid=2651557398&idx=1&sn=1fb991da ...
- leaflet的入门开发
2016年9月27日—1.0leaflet,最快的,最稳定和严谨的leaflet,终于出来了! leaflet是领先的开源JavaScript库为移动设备设计的互动地图.重33 KB的JS,所有映射大 ...
- Laravel 单设备登录
https://laravel-china.org/articles/10605/laravel-single-device-login 前几天在 laracasts 看了laravel5.6的新功能 ...
- 洛谷 P2568 GCD(莫比乌斯反演)
题意:$\sum_{i=1}^{n}\sum_{j=1}^{n}[gcd(i,j)\epsilon prime]$. 对于这类题一般就是枚举gcd,可得: =$\sum_{d\epsilon prim ...
- python 缓冲区 subprocess 黏包 黏包解决方案
一.缓冲区 二.两种黏包现象 两种黏包现象: 1 连续的小包可能会被优化算法给组合到一起进行发送 黏包现象1客户端 import socket BUFSIZE = 1024 ip_prort = (' ...
- Python学习之路15☞socket编程
一 客户端/服务器架构 即C/S架构,包括 1.硬件C/S架构(打印机) 2.软件C/S架构(web服务) C/S架构与socket的关系: 我们学习socket就是为了完成C/S架构的开发 二 os ...