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

【题意】



给你n个点m条边;

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

形成一个生成树;

(即让n个点都联通);

然后,你有S的预算;

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

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

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

【题解】

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

【Number Of WA】



0



【完整代码】

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define lson l,m,rt<<1
  4. #define rson m+1,r,rt<<1|1
  5. #define LL long long
  6. #define rep1(i,a,b) for (int i = a;i <= b;i++)
  7. #define rep2(i,a,b) for (int i = a;i >= b;i--)
  8. #define mp make_pair
  9. #define pb push_back
  10. #define fi first
  11. #define se second
  12. #define ms(x,y) memset(x,y,sizeof x)
  13. typedef pair<int,int> pii;
  14. typedef pair<LL,LL> pll;
  15. const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
  16. const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
  17. const double pi = acos(-1.0);
  18. const int M = 2e5+100;
  19. const int INF = 21e8;
  20. struct abc
  21. {
  22. int w,c,u,v,id;
  23. };
  24. int n,m,f[M],b[M],fa[M][22],d[M][22],deep[M],C,num,bin[22],bb[M],pri[M];
  25. LL tot,ans,S;
  26. abc a[M];
  27. vector <pii> g[M];
  28. int ff(int x)
  29. {
  30. if (f[x]==x)
  31. return x;
  32. else
  33. return f[x] = ff(f[x]);
  34. }
  35. int maxw(int x,int y)
  36. {
  37. if (a[x].w>a[y].w)
  38. return x;
  39. else
  40. return y;
  41. }
  42. void dfs(int x)
  43. {
  44. rep1(i,1,20)
  45. fa[x][i] = fa[fa[x][i-1]][i-1];
  46. rep1(i,1,20)
  47. d[x][i] = maxw(d[x][i-1],d[fa[x][i-1]][i-1]);
  48. for (pii temp:g[x])
  49. {
  50. int y = temp.fi,w = temp.se;
  51. if (y==fa[x][0]) continue;
  52. fa[y][0] = x,d[y][0] = w;
  53. deep[y] = deep[x]+1;
  54. dfs(y);
  55. }
  56. }
  57. int lcq(int x,int y)
  58. {
  59. if (deep[x]<deep[y])
  60. swap(x,y);
  61. //deep[x]>=deep[y];
  62. int temp = deep[x]-deep[y];
  63. rep1(i,0,20)
  64. if (temp&bin[i])
  65. x = fa[x][i];
  66. //deep[x]==deep[y];
  67. rep2(i,20,0)
  68. if (fa[x][i]!=fa[y][i])
  69. x = fa[x][i],y = fa[y][i];
  70. return x==y?x:fa[x][0];
  71. }
  72. int query(int x,int y)
  73. {
  74. //deep[x]>=deep[y]
  75. int temp = deep[x]-deep[y];
  76. int ret = 0;
  77. rep1(i,0,20)
  78. if (temp&bin[i])
  79. {
  80. ret = maxw(ret,d[x][i]);
  81. x = fa[x][i];
  82. }
  83. return ret;
  84. }
  85. int main()
  86. {
  87. //freopen("F:\\rush.txt","r",stdin);
  88. ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
  89. //init??????
  90. bin[0] = 1;
  91. rep1(i,1,20) bin[i] = bin[i-1]<<1;
  92. cin >> n >> m;
  93. rep1(i,1,m) cin >> a[i].w;
  94. rep1(i,1,m) cin >> a[i].c;
  95. rep1(i,1,m)
  96. {
  97. cin >> a[i].u >> a[i].v;
  98. a[i].id = i;
  99. }
  100. cin >>S;
  101. sort(a+1,a+1+m,[&](abc a,abc b) {return a.w<b.w;});
  102. C = INF;
  103. rep1(i,1,n) f[i] = i;
  104. rep1(i,1,m)
  105. {
  106. int x = a[i].u,y = a[i].v;
  107. int r1 = ff(x),r2 = ff(y);
  108. if (r1!=r2)
  109. {
  110. f[r1] = r2;
  111. g[x].pb(mp(y,i));
  112. g[y].pb(mp(x,i));
  113. b[i] = 1;
  114. if (a[i].c<C)
  115. {
  116. C = a[i].c;
  117. num = i;
  118. }
  119. tot+=a[i].w;
  120. }
  121. }
  122. ans = tot-S/C;
  123. dfs(1);
  124. int dl = 0,prei = 0;
  125. rep1(i,1,m)
  126. if (a[i].c<C)
  127. {
  128. int x = a[i].u,y = a[i].v;
  129. int z = lcq(x,y);
  130. int tmp = maxw(query(x,z),query(y,z));
  131. LL temp = tot-a[tmp].w+a[i].w;
  132. temp-=S/a[i].c;
  133. if (temp<ans)
  134. {
  135. b[dl] = 1;b[dl=tmp] = 0;
  136. b[prei] = 0;prei = i;
  137. num = i;b[i] = 1;
  138. ans = temp;
  139. }
  140. }
  141. cout << ans << endl;
  142. rep1(i,1,m)
  143. if (b[i])
  144. {
  145. bb[a[i].id] = 1;
  146. if (num==i)
  147. pri[a[i].id] = a[i].w-S/a[i].c;
  148. else
  149. pri[a[i].id] = a[i].w;
  150. }
  151. rep1(i,1,m)
  152. if (bb[i])
  153. cout << i <<' '<<pri[i]<<endl;
  154. return 0;
  155. }

【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. CodeForcesGym 100641D Generalized Roman Numerals

    Generalized Roman Numerals Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on  ...

  2. nodejs-循环

    for循环 for(var key in object){ consol.log('wor' : key); } 来自为知笔记(Wiz)

  3. 关于部门后端所有转向java前初步设想

    Java服务有些什么形式?眼下来看主要是下面几类: 1.  执行在Web应用server的Servlet 2.  Thrift.PB.Avro等相似框架写的java服务 3.  WebService( ...

  4. HttpClient学习系列 -- 学习总结

    jar包: HttpClient 4.x版本 简要介绍 HttpComponents 包括 HttpCore包和HttpClient包 HttpClient:Http的执行http请求 Default ...

  5. tensorflow利用预训练模型进行目标检测(一):安装tensorflow detection api

    一.tensorflow安装 首先系统中已经安装了两个版本的tensorflow,一个是通过keras安装的, 一个是按照官网教程https://www.tensorflow.org/install/ ...

  6. 用select拼接insert into,单引号转义

    SELECT 'INSERT INTO dbo.CMS_Transformation ( TransformationName , TransformationCode , Transformatio ...

  7. Pocket英语语法---六、感官动词接不同的动词表示什么意思

    Pocket英语语法---六.感官动词接不同的动词表示什么意思 一.总结 一句话总结:其实进行时一般是表示连续,动词原形一般表示常态,过去分词一般表示被动(或者完成). 感官动词接原型表示动作的一般情 ...

  8. Jenkins+Docker部署Maven聚合工程

    这几天,把公司的预发布环境,改成docker部署,遇到了一些坑,有jenkins里的部署脚本的问题,也有harbor仓库的问题,还有docker远程访问的问题,还有DooD....一堆坑 Jenkin ...

  9. ubuntu下无法将iNode绑定到侧边栏的解决办法

    title: ubuntu下无法将iNode绑定到侧边栏的解决办法 toc: false date: 2018-09-01 17:43:52 categories: methods tags: ubu ...

  10. Qt-上位机-串口助手

    前言:参考自:https://blog.csdn.net/u014695839/article/details/50611549 一.新建Widgets Appliaction工程 二.设计ui界面 ...