Codeforces Round #378 (Div. 2)F
题目:一个带权连通无向图,给第i条边权值减1需要花费ci元,你一共有S元,求最小生成树。
容易得出钱全部花在一条边上是最优的。
我们先做一遍最小生成树。
然后我们枚举减哪一条边。
如果这条边是树上的,那么直接得出答案。
如果不是,我们可以用这一条边去替换u[i]、v[i]路径之间任意一条。所以我们用倍增(我sb了用的树链剖分)找到路径上最大的那一条替换,计算答案。
最后把这条边放进树里,再求一遍最小生成树就能输出方案了。
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAX(a,b) a>b?a:b
#define N 210000
#define M 210000
#define mem(a) memset(a,0,sizeof(a))
#define rep(i,n) for (int i=1;i<=(n);i++)
#define LL long long
using namespace std;
int uu,vv,root,ee,rr,ll,_,n,tot,e[M<<],head[N],nex[N<<];
int id,p,q,m,top[N],siz[N],s[N],d[N],w[N],f[N],l[N<<],r[N<<];
LL fuck,a[N<<],orz,sum,S,ans,ma;
bool intree[M];
struct lbz
{
int u,v,d;
LL w,c;
}ed[M];
void add(int u,int v,int c)
{
e[++ee]=v;nex[ee]=head[u];head[u]=ee;
}
void build(int s,int ll,int rr)
{
l[s]=ll;r[s]=rr;
if (ll==rr)
a[s]=;
else
{
int mid=(ll+rr)>>;
build(s<<,ll,mid);
build((s<<)+,mid+,rr);
}
}
void add(int s)
{
if (l[s]==r[s])
a[s]=rr;
else
{
if (r[s<<]>=ll)
add(s<<);
else
add((s<<)+);
a[s]=MAX(a[s<<],a[(s<<)+]);
}
}
void sea(int s)
{
if (l[s]>rr || r[s]<ll)
return;
if (l[s]>=ll&&r[s]<=rr)
ans=MAX(ans,a[s]);
else
{
sea(s<<);
sea((s<<)+);
}
}
void dfs1(int u)
{
int j=head[u];
siz[u]=;
while (j>)
{
if (d[e[j]]==)
{
d[e[j]]=d[u]+;
f[e[j]]=u;
dfs1(e[j]);
if (siz[e[j]]>siz[s[u]])
s[u]=e[j];
siz[u]+=siz[e[j]];
}
j=nex[j];
}
}
void dfs2(int u)
{
int j=head[u];
if (s[u]!=)
{
w[s[u]]=++tot;
top[s[u]]=top[u];
dfs2(s[u]);
}
while (j>)
{
if (e[j]!=s[u]&&e[j]!=f[u])
{
w[e[j]]=++tot;
top[e[j]]=e[j];
dfs2(e[j]);
}
j=nex[j];
}
}
void init()
{
orz=sum;
id=;
mem(f);
}
bool cmp(lbz a,lbz b)
{
return a.w<b.w;
}
int find(int x)
{
if (f[x]==) return x;
f[x]=find(f[x]);
return f[x];
}
int main()
{
scanf("%d%d",&n,&m);
rep(i,m)
scanf("%I64d",&ed[i].w);
rep(i,m)
scanf("%I64d",&ed[i].c);
rep(i,m)
scanf("%d%d",&ed[i].u,&ed[i].v);
scanf("%I64d",&S);
rep(i,m)
ed[i].d=i;
sort(ed+,ed++m,cmp);
rep(i,m)
{
p=find(ed[i].u);
q=find(ed[i].v);
if (p!=q)
{
f[p]=q;
intree[i]=;
sum+=ed[i].w;
}
}
init();
rep(i,m)
if (intree[i]==)
{
add(ed[i].u,ed[i].v,ed[i].w);
add(ed[i].v,ed[i].u,ed[i].w);
}
build(,,n-);
root=;d[root]=;top[root]=root;
dfs1(root);
dfs2(root);
rep(i,m)
{
if (intree[i]==)continue;
if (d[ed[i].u]>d[ed[i].v])
swap(ed[i].u,ed[i].v);
ll=w[ed[i].v];rr=ed[i].w;
add();
}
rep(i,m)
{
if (intree[i]==)
{
fuck=sum-S/ed[i].c;
if (fuck<orz)
{
orz=fuck;
id=i;
}
continue;
}
uu=ed[i].u;vv=ed[i].v;
ma=;
while (top[uu]!=top[vv])
{
if (d[top[uu]]<d[top[vv]])
swap(uu,vv);
ans=;ll=w[top[uu]];rr=w[uu];sea();
ma=MAX(ma,ans);
uu=f[top[uu]];
}
if (uu!=vv)
{
if (d[uu]<d[vv]) swap(uu,vv);
ans=;ll=w[s[vv]];rr=w[uu];sea();
ma=MAX(ma,ans);
}
fuck=sum-ma+ed[i].w-S/ed[i].c;
if (fuck<orz)
{
orz=fuck;
id=i;
}
}
mem(f);mem(intree);
ed[id].w-=S/ed[id].c;
sort(ed+,ed++m,cmp);
rep(i,m)
{
p=find(ed[i].u);
q=find(ed[i].v);
if (p!=q)
{
f[p]=q;
intree[i]=;
}
}
printf("%I64d\n",orz);
rep(i,m)
if (intree[i])
printf("%d %I64d\n",ed[i].d,ed[i].w);
return ; }
| # | When | Who | Problem | Lang | Verdict | Time | Memory |
|---|---|---|---|---|---|---|---|
| 22090610 | 2016-11-07 11:28:10 | lbz007 | F - Drivers Dissatisfaction | GNU C++ | Accepted | 608 ms | 34700 KB |
Codeforces Round #378 (Div. 2)F的更多相关文章
- Codeforces Round #378 (Div. 2)F - Drivers Dissatisfaction GNU
http://codeforces.com/contest/733/problem/F 题意:给你一些城市和一些路,每条路有不满意程度和每减少一点不满意程度的花费,给出最大花费,要求找出花费小于s的最 ...
- Codeforces Round #378 (Div. 2) F - Drivers Dissatisfaction
F - Drivers Dissatisfaction 题目大意:给你n个点,m条边,每个边都有一个权重w,每条边也有一个c表示,消耗c元可以把这条边的权重减1,求最多消耗s元的最小生成树. 思路:因 ...
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- Codeforces Round #501 (Div. 3) F. Bracket Substring
题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Codeforces Round #378 (Div. 2) D题(data structure)解题报告
题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...
- Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)
题目链接:http://codeforces.com/contest/731/problem/F 题意:有n个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的 ...
- Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)
题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...
随机推荐
- SPSS|Data|Transfer|Analysis|Label|One sample test|Testval|Criables|
生物统计与实验设计-使用SPSS Data用于整合:Transfer用于预处理:Analysis用于数据的二维呈现:Label是在报表中呈现的名字: 给离散值编码: 对于离散值做数学计算: 均值比较用 ...
- jprofiler监控tomcat
jprofiler监控tomcat https://www.cnblogs.com/yjd_hycf_space/p/7727757.html https://www.jianshu.com/p/c3 ...
- 配置Maven本地仓库
以本机为例: 系统:Windows 开发工具:IDEA 如果想在dos窗口输mvn命令,需配置环境变量. 1. 在D盘新建repository文件夹,该目录用作maven的本地库. 2. 打开D:\P ...
- 陪诊App,陪孝子找回人性
2015年开始,越来越多的陪诊App开始上线,纷纷迎来了自己的第一批用户,同时,也迎来了大量资本的青睐,有些名气的问诊app甚至能单轮获得超过1000万元的融资,事实上,投资者是否青睐,简直就是中国新 ...
- elasticsearch 产生未分配分片的原因(es官网)
Reasons for unassigned shard: These are the possible reasons for a shard to be in a unassigned state ...
- 使用Handler的步骤
使用Handler的大致流程: 1.首先创建一个Handler对象,可以直接使用Handler无参构造函数创建Handler对象,也可以继承Hander类,重写HandleMessage方法来创建Ha ...
- 苹果为何要一定要去印度生产iPhone
现在,关于苹果手机有几种流行的猜想和期待,今年恰逢iPhone问世十周年,新产品估计会有颠覆性创新,消费者正望穿秋水,翘首企盼,但只需待到金秋便可知晓,何况iPhone8或许也就是一小撮发烧友的选 ...
- 一道二叉树题的n步优化——LeetCode98validate binary search tree(草稿)
树的题目,往往可以用到三种遍历.以及递归,因为其结构上天然地可以往深处递归,且判断条件也往往不复杂(左右子树都是空的). LeetCode 98题讲的是,判断一棵树是不是二叉搜索树. 题目中给的是标准 ...
- mysql插入数据报错一二
上周selenium+phantomjs+python3简单爬取一个网站,往数据库写数据遇到以下两个问题,记录一下: 报错一:Data truncated for column 'update_tim ...
- nginx设置目录浏览及中文乱码问题解决
在Nginx下默认是不允许列出整个目录的.如需此功能, 先打开nginx.conf文件,在location server 或 http段中加入 autoindex on;另外两个参数最好也加上去: a ...