题目:一个带权连通无向图,给第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的更多相关文章

  1. Codeforces Round #378 (Div. 2)F - Drivers Dissatisfaction GNU

    http://codeforces.com/contest/733/problem/F 题意:给你一些城市和一些路,每条路有不满意程度和每减少一点不满意程度的花费,给出最大花费,要求找出花费小于s的最 ...

  2. Codeforces Round #378 (Div. 2) F - Drivers Dissatisfaction

    F - Drivers Dissatisfaction 题目大意:给你n个点,m条边,每个边都有一个权重w,每条边也有一个c表示,消耗c元可以把这条边的权重减1,求最多消耗s元的最小生成树. 思路:因 ...

  3. 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. Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

    Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

  5. Codeforces Round #501 (Div. 3) F. Bracket Substring

    题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...

  6. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  7. Codeforces Round #378 (Div. 2) D题(data structure)解题报告

    题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...

  8. Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)

    题目链接:http://codeforces.com/contest/731/problem/F 题意:有n个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的 ...

  9. Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)

    题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...

随机推荐

  1. LG_3459_[POI2007]MEG-Megalopolis

    题目描述 Byteotia has been eventually touched by globalisation, and so has Byteasar the Postman, who onc ...

  2. iOS(Swift)学习笔记之SwiftyJSON的使用

    本文为原创文章,转载请标明出处 1. 通过CocoaPods安装SwiftyJSON platform :ios, '10.0' target '<Your Target Name>' d ...

  3. win7电脑磁盘文件以分组方式展现解决方案

    问题: win7电脑磁盘文件以分组方式展现,具体如下: 解决: 右键磁盘空白处-->分组依据-->选择无:图解如下:

  4. HTMLTestRunner测试报告

    把测试报告写入文件中,设置报告生成的路径 测试报告名称上添加时间 HTMLTestRunner文件如下,复制即可用,把该文件放在Lib下即可 """ A TestRunn ...

  5. ERROR 1129 (00000) Host ‘XXXXXX’ is blocked because of many connection errors; unblock with ‘mysqlad

    1.今天早上由于公司网络带宽达到上限,导致多台web服务器连接mysql服务器超时.后来情况好转后,连接数据库服务器出现如下错误. Host '*' is blocked because of man ...

  6. Gre 隧道与 Keepalived

    这一篇文章是做了不少功课的. 什么是 Gre 隧道 什么是 Vrrp KeepAlived 是什么 用Keepalived 怎么玩 附录 什么是 Gre 隧道 GRE 隧道是一种 IP-2-IP 的隧 ...

  7. 分析Android中View的工作流程

    在分析View的工作流程时,需要先分析一个很重要的类,MeasureSpec.这个类在View的测量(Measure)过程中会用到. MeasureSpec MeasureSpec是View的静态内部 ...

  8. LeetCode 232题用栈实现队列(Implement Queue using Stacks) Java语言求解

    题目链接 https://leetcode-cn.com/problems/implement-queue-using-stacks/ 题目描述 使用栈实现队列的下列操作: push(x) -- 将一 ...

  9. 关于C++类中的三兄弟(pretect、private、public)

    1.public修饰的成员变量 在程序的任何地方都可以被访问,就是公共变量的意思,不需要通过成员函数就可以由类的实例直接访问 2.private修饰的成员变量 只有类内可直接访问,私有的,类的实例要通 ...

  10. 1——PHP常见的系统常量

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...