【题目链接】:http://codeforces.com/problemset/problem/733/F

【题意】



给你n个点m条边;

让你从中选出n-1条边;

形成一个生成树;

(即让n个点都联通);

然后,你有S的预算;

每次可以选择一条边i,然后花费ci的预算,把这条边的权值递减1;

(边一开始的权值为wi);

问你最后的最小生成树是多少;

【题解】

/*
肯定是找某一条边一直减(ci最小的那一个,因为代价最小,又都是减少1);
把m条边按照w升序排;
做个最小生成树;
把最小生成树里面c最小的那条边一直减就好,这个作为ans1;
然后在这个最小生成树上写个dfs,
搞出来每个点上面的第2^i个节点是谁,以及这个点到这个点上面的第2^i个点之间,最大的w是哪条边.
然后枚举所有的非树边,这里的非树边,它的c的值一定要小于最小生成树里面的树边的最小的c值;
这样它才有可能成为那条一直减小的边;然后最后比最小生成树里面的某条边边权来得小;
假如非树边的两个点是u和v;
则如果你要把u-v这条边加到MST里面的话,则必然要在从u到v的路径上选一条边删掉;
删掉的边应是最大的那条边.(记录这条边是什么,删掉之后下次如果还要删的话,得还原)
具体方式就是找到u和v的LCA;
然后从u到lca的路径中找最大w的边;
然后从v到lca的路径总找最大w的边;
取两个边的w的较大者;
然后把这条边删掉;
然后把这条枚举的边加进去;一直减;然后更新答案;
*/

【Number Of WA】



0



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int M = 2e5+100;
const int INF = 21e8; struct abc
{
int w,c,u,v,id;
}; int n,m,f[M],b[M],fa[M][22],d[M][22],deep[M],C,num,bin[22],bb[M],pri[M];
LL tot,ans,S;
abc a[M];
vector <pii> g[M]; int ff(int x)
{
if (f[x]==x)
return x;
else
return f[x] = ff(f[x]);
} int maxw(int x,int y)
{
if (a[x].w>a[y].w)
return x;
else
return y;
} void dfs(int x)
{
rep1(i,1,20)
fa[x][i] = fa[fa[x][i-1]][i-1];
rep1(i,1,20)
d[x][i] = maxw(d[x][i-1],d[fa[x][i-1]][i-1]);
for (pii temp:g[x])
{
int y = temp.fi,w = temp.se;
if (y==fa[x][0]) continue;
fa[y][0] = x,d[y][0] = w;
deep[y] = deep[x]+1;
dfs(y);
}
} int lcq(int x,int y)
{
if (deep[x]<deep[y])
swap(x,y);
//deep[x]>=deep[y];
int temp = deep[x]-deep[y];
rep1(i,0,20)
if (temp&bin[i])
x = fa[x][i];
//deep[x]==deep[y];
rep2(i,20,0)
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 y)
{
//deep[x]>=deep[y]
int temp = deep[x]-deep[y];
int ret = 0;
rep1(i,0,20)
if (temp&bin[i])
{
ret = maxw(ret,d[x][i]);
x = fa[x][i];
}
return ret;
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
//init??????
bin[0] = 1;
rep1(i,1,20) bin[i] = bin[i-1]<<1;
cin >> n >> m;
rep1(i,1,m) cin >> a[i].w;
rep1(i,1,m) cin >> a[i].c;
rep1(i,1,m)
{
cin >> a[i].u >> a[i].v;
a[i].id = i;
}
cin >>S;
sort(a+1,a+1+m,[&](abc a,abc b) {return a.w<b.w;});
C = INF;
rep1(i,1,n) f[i] = i;
rep1(i,1,m)
{
int x = a[i].u,y = a[i].v;
int r1 = ff(x),r2 = ff(y);
if (r1!=r2)
{
f[r1] = r2;
g[x].pb(mp(y,i));
g[y].pb(mp(x,i));
b[i] = 1;
if (a[i].c<C)
{
C = a[i].c;
num = i;
}
tot+=a[i].w;
}
}
ans = tot-S/C;
dfs(1);
int dl = 0,prei = 0;
rep1(i,1,m)
if (a[i].c<C)
{
int x = a[i].u,y = a[i].v;
int z = lcq(x,y);
int tmp = maxw(query(x,z),query(y,z));
LL temp = tot-a[tmp].w+a[i].w;
temp-=S/a[i].c;
if (temp<ans)
{
b[dl] = 1;b[dl=tmp] = 0;
b[prei] = 0;prei = i;
num = i;b[i] = 1;
ans = temp;
}
}
cout << ans << endl;
rep1(i,1,m)
if (b[i])
{
bb[a[i].id] = 1;
if (num==i)
pri[a[i].id] = a[i].w-S/a[i].c;
else
pri[a[i].id] = a[i].w;
}
rep1(i,1,m)
if (bb[i])
cout << i <<' '<<pri[i]<<endl;
return 0;
}

【codeforces 733F】Drivers Dissatisfaction的更多相关文章

  1. 【codeforces 733F】 Drivers Dissatisfaction

    http://codeforces.com/problemset/problem/733/F (题目链接) 题意 给出一张n个点的无向图,每一条变有两个特征值:${w,c}$:分别表示这条边的权值为$ ...

  2. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  3. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  4. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  5. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  6. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  7. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  8. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  9. 【Codeforces 670C】 Cinema

    [题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...

随机推荐

  1. HDU5514 Frogs

    /* HDU5514 Frogs http://acm.hdu.edu.cn/showproblem.php?pid=5514 容斥原理 * * */ #include <cstdio> ...

  2. lpa标签传播算法解说及代码实现

    package lpa; import java.util.Arrays; import java.util.HashMap; import java.util.Map; public class L ...

  3. osEye.Net:离别是为了将来的重逢

    这一时刻已经成为osEye历史..... 在热心网友的关怀和鼓励之下,osEye.net将继续运行着,感谢你们陪伴osEye一起走过.... 与你相知相恋已经有4个年头了,你的成长到成熟都让我历历在目 ...

  4. 结束QQ即时通信垄断,开辟即时通信互联互通instantnet时代

    结束QQ即时通信垄断,开辟即时通信互联互通instantnet时代 蓬勃发展的即时通信产业 即时通信(IM)是指可以即时发送和接收互联网消息等的业务. 即时通信.就是瞬间把信息发送给对方,假设不是即时 ...

  5. 2015.05.12,外语,读书笔记-《Word Power Made Easy》 15 “如何谈论不同人的特点” SESSION 45

    TEASER PREVIEW 以-ous结尾的,描绘某人特点的词语包括: fawning(['fɔ:niŋ] adj.奉承的),servilely(['sә:vail] adj. 卑屈的, 奴隶的) ...

  6. 30.QT IDE编写

    mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QTe ...

  7. MarkDown 语法备份

    标题 标题1 标题2 标题3 标题4 标题5 无序列表 飞雪连天射白鹿 书神侠倚碧鸳 有序列表 飞雪连天射白鹿 笑书神侠倚碧鸳 超链接 百度 图片 粗体和斜体 粗体 斜体 表格 左对齐标题 右对齐标题 ...

  8. 服务端 | Linux 学习总结 (一)

    http://billie66.github.io/TLCL/book/ 1.Ubuntu && linux shell 命令 Ubuntu两个重要版本:12.04和14.04 在终端 ...

  9. 问题集锦 ~ PHP

    #switch //当variable为数字0的时候,case为true,会执行第一段case代switch (variable) { case 'value': # code... break; d ...

  10. table-layout:fixed属性

    说实话,第一次见到这个属性有点懵圈,真是没见过这个属性 好了,直接说作用 table-layout其实是一种表格算法,用来显示表格单元格.行.列的算法规则. 固定表格布局: 固定表格布局与自动表格布局 ...