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之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...
随机推荐
- 使用Spring AOP 实现日志管理(简单教程)
有时候,我们在做项目时会遇到这样的需求: 给XXX.java中的所有方法加上指定格式的日志输出. 针对这种指定类.或者指定方法进行共性操作的功能,我们完全可以使用Spring AOP来实现. 本文使用 ...
- python3下应用requests
模拟浏览器请求有两种,一种是不需要用户登录或者验证的请求,一种是需要用户登录或者验证的请求 那么我们先来说说不需要用户登录的方法 这种方式直接可以获取源码,用get的请求方式 登录的方式 获取这种页面 ...
- SHELL的判断括号区别
在使用if语句时,经常会使用().(()).[].[[]].{}等括号,如下为几种括号简单区别对比: ( ) 用于多个命令组.命令替换.初始化数组: (( )) 整数扩展.运算符.重定义变量值,算术运 ...
- textarea 是否换行的问题解决
需求:判断当前textarea是否已经换行(这个换行有2种方式:1.不断输入文字直到超过指定宽度后自动换行:2.按了回车以后进行换行) 单纯的解决第二种换行很简单.网上提供了很多常规的解决方案. De ...
- 编译gcc报错make[3]: Leaving directory `/usr/local/src/gcc-7.4.0/build/gcc' make[2]: *** [all-stage1-gcc] Error 2 处理
因业务需要安装7.4高版本gcc时报错: configure: error: in `/usr/local/src/gcc-7.4.0/build/gcc': configure: error: C+ ...
- MUI 提问框多个按钮的回调函数
var btns = new Array("按钮1", "按钮2"); mui.confirm("这是信息", "这是标题&quo ...
- MyBatis XML 配置文件 properties 元素扩展
在分析 MyBatis XML 配置文件 properties 元素时提到了三种配置方式,其中 property 子元素 和 properties 文件都比较容易理解,但是为什么还要提供一种代码参数传 ...
- Soldier and Badges
题目链接:https://vjudge.net/problem/CodeForces-546B AC代码: #include<iostream> #include<algorithm ...
- nginx: [emerg] unknown directive “ ” in /usr/local/nginx/conf/vhost/XXX.conf:53报错处理
开发同事发给我一小段nginx配置,加到服务器上之后,执行nginx -s reload时,出现报错: nginx: [emerg] unknown directive “ ” in /usr/loc ...
- Python scan查找Redis集群中的key
import redis import sys from rediscluster import StrictRedisCluster #host = "172.17.155.118&quo ...