【codeforces 733F】 Drivers Dissatisfaction
http://codeforces.com/problemset/problem/733/F (题目链接)
题意
给出一张n个点的无向图,每一条变有两个特征值:${w,c}$;分别表示这条边的权值为${w}$,每将这条边的权值减小1需要充${c}$元钱。初始时有${S}$元钱,你可以对任意边充钱使得它的权值${w}$减少,甚至减成负数。现在需要你选出若干条边,使得所有的点都联通并且这些边的权值和在“充了钱”之后最小。
Solution
codeforces上的题目真是良心题。
想一想,假设我们已经选出了一些边,如果我们“充钱”,反正充哪条边都是只能使最终答案减小1,不如就抓着其中一条${c}$最小的边一通狂充,不充钱还想跟我玩?于是我们就很容易得到一个初始想法:跑一遍关于${w}$的最小生成树,然后狂减${c}$最小的边,得到一个初始答案${ans}$。
考虑这样一种情况:可能存在一条不在最小生成树的边,但是它的${c}$比最小生成树中的边的${c}$都要小,我们可以将它选入答案并通过对它“充钱”,使答案更优。
这样的情况怎么处理呢?我们枚举每一条不在最小生成树中并且${c}$符合要求的边,尝试将它加入生成树中。加入一条边${(u,v)}$,那么必定要删掉树上一条边,那么删去的这条边一定是树上${u,v}$间${w}$最大的一条边,此时我们不用考虑树中的边的${c}$,因为如果我们要“充钱”一定是对新加入的这条边充,如果最后剩下的钱小于新加入这条边的${c}$,那么显然剩下的钱已经不能再做出贡献了。
那么现在问题就是如何快速查询树上两点间的最长边,是不是很熟悉,没错就是NOIP2013货车运输,直接树上倍增即可。
细节
记得开long long。然后方案的统计有点麻烦,注意不要写错,Wa on test 100,莫名其妙过了前面99个点,这是要FST的节奏啊→_→。
代码
// codeforces 733F
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define MOD 99999997
#define inf 2000000000
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; const int maxn=200010;
struct edge {int to,next,w,id;}e[maxn<<1];
struct data {int u,v,w,c,id;}a[maxn];
int f[maxn],head[maxn],fa[maxn][30],bin[30],d[maxn][30],deep[maxn],b[maxn];
int cnt,S,n,m,prt[maxn],pp[maxn]; bool cmp(data a,data b) {
return a.w<b.w;
}
int maxw(int x,int y) {
return a[x].w<a[y].w ? y : x;
}
int find(int x) {
return x==f[x] ? x : f[x]=find(f[x]);
}
void link(int u,int v,int w) {
e[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;e[cnt].w=w;
e[++cnt].to=u;e[cnt].next=head[v];head[v]=cnt;e[cnt].w=w;
}
void dfs(int x) {
for (int i=1;i<=20;i++) fa[x][i]=fa[fa[x][i-1]][i-1];
for (int i=1;i<=20;i++) d[x][i]=maxw(d[x][i-1],d[fa[x][i-1]][i-1]);
for (int i=head[x];i;i=e[i].next) if (e[i].to!=fa[x][0]) {
deep[e[i].to]=deep[x]+1;
d[e[i].to][0]=e[i].w;
fa[e[i].to][0]=x;
dfs(e[i].to);
}
}
int lca(int x,int y) {
if (deep[x]<deep[y]) swap(x,y);
int t=deep[x]-deep[y];
for (int i=0;bin[i]<=t;i++) if (bin[i]&t) x=fa[x][i];
for (int i=20;i>=0;i--) if (fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
return x==y ? x : fa[x][0];
}
int query(int x,int f) {
int t=deep[x]-deep[f];
int res=0;
for (int i=0;bin[i]<=t;i++) if (bin[i]&t) res=maxw(res,d[x][i]),x=fa[x][i];
return res;
}
int main() {
bin[0]=1;for (int i=1;i<=20;i++) bin[i]=bin[i-1]<<1;
scanf("%d%d",&n,&m);
for (int i=1;i<=m;i++) scanf("%d",&a[i].w);
for (int i=1;i<=m;i++) scanf("%d",&a[i].c);
for (int i=1;i<=m;i++) scanf("%d%d",&a[i].u,&a[i].v),a[i].id=i;
scanf("%d",&S);
sort(a+1,a+1+m,cmp);
for (int i=1;i<=n;i++) f[i]=i;
int C=inf;
LL ans=0,tot=0;int num=0,dl=0;
for (int i=1;i<=m;i++) {
int r1=find(a[i].u),r2=find(a[i].v);
if (r1!=r2) {
link(a[i].u,a[i].v,i);
f[r1]=r2;
b[i]=1;
tot+=(LL)a[i].w;
if (a[i].c<C) C=a[i].c,num=i;
}
}
ans=tot-S/C;
dfs(1);
for (int i=1;i<=m;i++) if (a[i].c<C) {
int ff=lca(a[i].u,a[i].v);
int x=maxw(query(a[i].u,ff),query(a[i].v,ff));
LL tmp=(LL)tot-a[x].w+a[i].w-S/a[i].c;
if (ans>tmp) {
ans=tmp;b[dl]=1;b[num]=0;b[dl=x]=0;num=i;b[i]=1;}
}
printf("%lld\n",ans);
for (int i=1;i<=m;i++) if (b[i]) {
int x=num==i ? S/a[i].c : 0;
prt[a[i].id]=a[i].w-x;
pp[a[i].id]=1;
}
for (int i=1;i<=m;i++) if (pp[i]) printf("%d %d\n",i,prt[i]);
return 0;
}
【codeforces 733F】 Drivers Dissatisfaction的更多相关文章
- 【codeforces 733F】Drivers Dissatisfaction
[题目链接]:http://codeforces.com/problemset/problem/733/F [题意] 给你n个点m条边; 让你从中选出n-1条边; 形成一个生成树; (即让n个点都联通 ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
随机推荐
- facebook graphql
思想先进,前端直接从后台调用所需要的数据. 最简单的理解: 从"select * from 学生表" 进化为"select name, sex from 学生表" ...
- K8 系统中省市县数据表的设计可以反映出什么? 通过一个基础业务表的设计品味软件系统的整体架构
1:没有严谨的Id思想,不变化的Id思想,看不见的Id的思想. 2:数据不严谨,没有上下级关系,没有树形结构,ParentId 的思想. 3:表之间的关系都是弱关联,基础数据一修改业务数据就容易乱套. ...
- c++ 副本构造器
我们都知道两个指针指向同一个变量时如果一个指针被释放那么另一个就会出问题 为了说明问题我做了一个很恶心的小例子 class C { public : C(int v) { ptrInt=new int ...
- 汤姆大叔的6道javascript编程题题解
看汤姆大叔的博文,其中有篇(猛戳这里)的最后有6道编程题,于是我也试试,大家都可以先试试. 1.找出数字数组中最大的元素(使用Math.max函数) var a = [1, 2, 3, 6, 5, 4 ...
- keepalived+LVS 实现双机热备、负载均衡、失效转移 高性能 高可用 高伸缩性 服务器集群
本章笔者亲自动手,使用LVS技术实现实现一个可以支持庞大访问量.高可用性.高伸缩性的服务器集群 在读本章之前,可能有不少读者尚未使用该技术,或者部分读者使用Nginx实现应用层的负载均衡.这里大家都可 ...
- gettter,delatter,settter
class Foo: x = 1 def __init__(self,y): self.y=y def __getattr__(self, item): print('自行--getatter') d ...
- CSS3绘制弹球动画效果
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- Oracle中可以nologging执行的操作
redo重做日志是Oracle数据库恢复(recovery)的基础:但在很多情况下可以通过禁用重做日志的产生来加速SQL语句的完成,也就是我们所说的可nologging化的操作,这些操作大多是或串行的 ...
- MySql错误1045 Access denied for user 'root'@'localhost' (using password:YES) windows下的解决方案(忘记密码)
1.进入管理员控制台停止mysql服务:net stop mysql; 2.进入mysql的安装路径,如我的安装路径为C:\Program Files\MySQL\MySQL Server 5.5,打 ...
- zabbix 用 LLD 完全自动化监控 Oracle
文章转载自:http://mp.weixin.qq.com/s?__biz=MzA3MzYwNjQ3NA==&mid=2651296856&idx=1&sn=2bdf78071 ...