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 ...
随机推荐
- Vue. 之 npm安装Element
Vue. 之 npm安装Element 前提: 相关运行环境以搭建完成,例如:Node.Npm等. 假如我的项目目录如下: D:\DISK WORKSPACE\VSCODE\CDS\cds-ap ...
- SPSS操作:轻松实现1:1倾向性评分匹配(PSM)
SPSS操作:轻松实现1:1倾向性评分匹配(PSM) 谈起临床研究,如何设立一个靠谱的对照,有时候成为整个研究成败的关键.对照设立的一个非常重要的原则就是可比性,简单说就是对照组除了研究因素外,其他的 ...
- mysql中的year(date)和date_format(date,format)的用法
执行:select SYSDATE() from dual; 返回:2017-10-24 13:48:06 执行:select DATE_FORMAT(SYSDATE(),'%Y.%m.%d') fr ...
- Directx11教程(4) 一个最基本D3D应用程序(2)
原文:Directx11教程(4) 一个最基本D3D应用程序(2) 接着上篇教程的代码,本篇加入基本的D3D代码,实现一个完整的D3D11程序框架. 我们增加一个新类D3DClass, 用来处理3D渲 ...
- oracle-ORA-01650错误
Unable to extend rollback segment 原因:没有足够的撤销空间用来处理所有活动事务
- Kubernetes Ingress日志分析入门
本文主要介绍如何基于日志服务构建Kubernetes Ingress日志分析平台,并提供一些简单的动手实验方便大家快速了解日志服务相关功能. 部署Ingress日志方案 登录容器服务管理控制台. 将上 ...
- tyvj1017 冗余关系
描述 Mrs.Chen是一个很认真很称职的语文老师 ......所以,当她看到学生作文里的人物关系描述得非常的麻烦的时候,她非常生气,于是宣布:凡是作文里有冗余关系的,一率罚抄出师表10次...同学们 ...
- SPARK-SQL内置函数之字符串函数
转载请注明转自:http://www.cnblogs.com/feiyumo/p/8763186.html 1.concat对于字符串进行拼接 concat(str1, str2, ..., strN ...
- js遮罩
1.1 背景半透明遮罩层样式 需要一个黑色(当然也可以其他)背景,且须设置为绝对定位,以下是项目中用到的css样式: /* 半透明的遮罩层 */ #overlay { background: #000 ...
- 【Django入坑之路】Form组件
1:From组件的简单使用 1创建From: #导入模块 from django import forms from django.forms import fields, widgets # 导入自 ...