题意:有n个点,m条边,每条边有不满意度w[i],以及减小一个不满意度代价c[i],问给你s元用来减少代价,找到一个总不满意度最小的生成树,保证有解。(减少后的不满意度可以为负数)
思路:
显然所有的钱都应该用在生成树中c最小的那条边上
先求出以w[i]为权的最小生成树O(nlogn)
答案一定是在现在求出的最小生成树基础上换掉一条边 或 不变
把所有边进行替换尝试,找到换掉后的最优解。(可以只尝试c不大于mst中最小的c的边)
添加一条边后需要尝试换掉,与该边组成环后的最大权的边,
找最大权就是找新增边的两个节点到最近公共祖先的路径上的最大边

具体见代码

  1. const int maxn = * + ;
  2.  
  3. struct Edge {
  4. int id, u, v, w, c;//从u到v权为w
  5. bool operator < (const Edge& rhs) const {
  6. if (w != rhs.w) return w < rhs.w;
  7. return id < rhs.id;
  8. }
  9. };
  10. vector<Edge> e;
  11. int n, m;
  12.  
  13. int pa[maxn];
  14. vector<pair<int, bool> > G[maxn];
  15. bool vis[maxn]; //被选中的边
  16. int find(int x) {
  17. return pa[x] != x ? pa[x] = find(pa[x]) : x;
  18. }
  19. int kruskal() {
  20. int min_c = INF;
  21. for (int i = ; i <= n; i++) pa[i] = i;
  22. sort(e.begin(), e.end());
  23. for (int i = ; i < e.size(); i++) {
  24. int x = find(e[i].u), y = find(e[i].v);
  25. if (x != y) {
  26. vis[e[i].id] = true;
  27. min_c = min(min_c, e[i].c);
  28. G[e[i].u].push_back(mp(i, ));
  29. G[e[i].v].push_back(mp(i, ));
  30. pa[x] = y;
  31. }
  32. }
  33. return min_c;
  34. }
  35.  
  36. const int maxlog = ;
  37.  
  38. int fa[maxn]; // 父亲数组
  39. int cost[maxn]; // 和父亲的费用
  40. int L[maxn]; // 层次(根节点层次为0)
  41. struct LCA {
  42. int anc[maxn][maxlog]; // anc[p][i]是结点p的第2^i级父亲。anc[i][0] = fa[i]
  43. int maxcost[maxn][maxlog]; // maxcost[p][i]是i和anc[p][i]的路径上的最大费用
  44. // 预处理,根据fa和cost数组求出anc和maxcost数组
  45. void preprocess() {
  46. for(int i = ; i <= n; i++) {
  47. anc[i][] = fa[i]; maxcost[i][] = cost[i];
  48. for(int j = ; ( << j) <= n; j++) anc[i][j] = -;
  49. }
  50. for(int j = ; ( << j) <= n; j++) {
  51. for(int i = ; i <= n; i++) {
  52. if(anc[i][j-] != -) {
  53. int a = anc[i][j-];
  54. anc[i][j] = anc[a][j-];
  55. maxcost[i][j] = max(maxcost[i][j-], maxcost[a][j-]);
  56. }
  57. }
  58. }
  59. }
  60. // 求p到q的路径上的最大权
  61. pii query(int p, int q) {
  62. int tmp, power, i;
  63. if(L[p] < L[q]) swap(p, q); //L[p] >= L[q]
  64. for(power = ; ( << power) <= L[p]; power++);
  65. power--; //(2^power <= L[p]中的最大的)
  66. int ans = -INF;
  67. for(int i = power; i >= ; i--) {
  68. if (L[p] - ( << i) >= L[q]) {
  69. ans = max(ans, maxcost[p][i]);
  70. p = anc[p][i];
  71. }
  72. }
  73. if (p == q) return mp(ans, p); // LCA为p
  74. for(int i = power; i >= ; i--) {
  75. if(anc[p][i] != - && anc[p][i] != anc[q][i]) {
  76. ans = max(ans, maxcost[p][i]); p = anc[p][i];
  77. ans = max(ans, maxcost[q][i]); q = anc[q][i];
  78. }
  79. }
  80. ans = max(ans, cost[p]);
  81. ans = max(ans, cost[q]);
  82. return mp(ans, fa[p]); // LCA为fa[p](它也等于fa[q])
  83. }
  84. } lca;
  85.  
  86. int w[maxn], c[maxn];
  87. int all;
  88.  
  89. void init()
  90. {
  91. scanf("%d%d", &n, &m);
  92. for (int i = ; i < m; i++)
  93. {
  94. scanf("%lld", w + i);
  95. }
  96. for (int i = ; i < m; i++)
  97. {
  98. scanf("%lld", c + i);
  99. }
  100. int u, v;
  101. for (int i = ; i < m; i++)
  102. {
  103. scanf("%d%d", &u, &v);
  104. e.push_back((Edge){i, u, v, w[i], c[i]});
  105. }
  106. scanf("%d", &all);
  107. }
  108.  
  109. void bfs()
  110. {
  111. queue<int> q;
  112. q.push();
  113. fa[] = ;
  114. L[] = ;
  115. cost[] = ;
  116. while (!q.empty())
  117. {
  118. int u = q.front(); q.pop();
  119. for (auto i : G[u])
  120. {
  121. int v = e[i.x].v;
  122. if (i.y) v = e[i.x].u;
  123. if (fa[u] == v)
  124. {
  125. continue;
  126. }
  127.  
  128. q.push(v);
  129. fa[v] = u;
  130. L[v] = L[u] + ;
  131. cost[v] = e[i.x].w;
  132. }
  133. }
  134. }
  135.  
  136. int find_idx(int u, int v) //找边uv的编号
  137. {
  138. if (u == -) cout << "error!" << endl;
  139. for (int i = ; i < G[u].size(); i++)
  140. {
  141. int to = e[G[u][i].x].v;
  142. if (G[u][i].y) to = e[G[u][i].x].u;
  143. if (to == v) return e[G[u][i].x].id;
  144. }
  145. }
  146.  
  147. Edge find_change(const int min_c, LL& sub)
  148. {
  149. Edge ans = (Edge){-};
  150. for (auto i : e)
  151. {
  152. if (vis[i.id] || i.c > min_c) continue;
  153. LL max_wc = lca.query(i.u, i.v).x;
  154. LL new_sub = all / i.c - i.w + max_wc;
  155. if (new_sub > sub)
  156. {
  157. sub = new_sub;
  158. ans = i;
  159. }
  160. }
  161. return ans;
  162. }
  163.  
  164. int find_delete(const Edge& ans)
  165. {
  166. pii father = lca.query(ans.u, ans.v);
  167. pii del(-, -); //删除 del
  168. for (int i = ans.u; i != father.y; i = fa[i])
  169. {
  170. if (cost[i] == father.x)
  171. {
  172. del = mp(i, fa[i]);
  173. break;
  174. }
  175. }
  176. if (del.x == -)
  177. {
  178. for (int i = ans.v; i != father.y; i = fa[i])
  179. {
  180. if (cost[i] == father.x)
  181. {
  182. del = mp(i, fa[i]);
  183. break;
  184. }
  185. }
  186. }
  187. return find_idx(del.x, del.y);
  188. }
  189.  
  190. void solve()
  191. {
  192. int min_c = kruskal();
  193. /*
  194. cout << "MST:" <<endl;
  195. for (auto i : e)
  196. if (vis[i.id]) cout << i.id +1 << " ";
  197. cout << endl;
  198. */
  199. bfs();
  200. lca.preprocess();
  201. LL sub = all / min_c;
  202. Edge ans = find_change(min_c, sub);
  203. LL sum = ;
  204. for (auto i : e)
  205. {
  206. if (vis[i.id]) sum += i.w;
  207. }
  208. printf("%lld\n", sum - sub);
  209. if (ans.id == -)
  210. {
  211. bool flag = true;
  212. for (auto i : e)
  213. {
  214. if (vis[i.id])
  215. {
  216. if (flag && i.c == min_c)
  217. {
  218. printf("%d %d\n", i.id + , i.w - all / i.c);
  219. flag = false;
  220. }
  221. else
  222. printf("%d %d\n", i.id + , i.w);
  223. }
  224. }
  225. return;
  226. }
  227.  
  228. int idx = find_delete(ans);
  229. for (auto i : e)
  230. {
  231. if (vis[i.id] && i.id != idx)
  232. {
  233. printf("%d %d\n", i.id + , i.w);
  234. }
  235. }
  236. printf("%d %d\n", ans.id + , ans.w - all/ans.c);
  237. }
  238.  
  239. int main()
  240. {
  241. init();
  242. solve();
  243. return ;
  244. }

Codeforces 733F Drivers Dissatisfaction的更多相关文章

  1. 【codeforces 733F】 Drivers Dissatisfaction

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

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

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

  3. Drivers Dissatisfaction

    Drivers Dissatisfaction time limit per test 4 seconds memory limit per test 256 megabytes input stan ...

  4. CF733F Drivers Dissatisfaction【链剖】【最小生成树应用】

    F. Drivers Dissatisfaction time limit per test 4 seconds memory limit per test 256 megabytes input s ...

  5. 【codeforces 733F】Drivers Dissatisfaction

    [题目链接]:http://codeforces.com/problemset/problem/733/F [题意] 给你n个点m条边; 让你从中选出n-1条边; 形成一个生成树; (即让n个点都联通 ...

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

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

  7. codeforce 378 div 2 F —— Drivers Dissatisfaction (最小生成树,LCA,倍增)

    官方题解: If you choose any n - 1 roads then price of reducing overall dissatisfaction is equal to min(c ...

  8. Drivers Dissatisfaction 最小生成树+LCA

    题意:给一张n个点m条边的连通图,每条边(ai,bi)有一个权值wi和费用ci, 表示这条边每降低1的权值需要ci的花费.现在一共有S费用可以用来降低某些边的权值 (可以降到负数),求图中的一棵权值和 ...

  9. 【CF733F】Drivers Dissatisfaction(最小瓶颈生成树,倍增)

    题意:给出一个图,每条边有权值和花费c,每次花费c能使的权值-1.给出一个预算,求减完权值后的一个最小生成树. 思路:感谢CC大神 有这样一个结论:最佳方案里必定存在一种,预算全部花费全部分配在一条边 ...

随机推荐

  1. nodejs 访问mysql

    安装 $ npm install mysql 简介 这个一个mysql的nodejs版本的驱动,是用JavaScript来编写的.不需要编译 这儿有个例子来示范如何使用: var mysql = re ...

  2. Java面向对象三大特点之封装

    封装 含义:将对象的属性和行为封装起来,而将对象的属性和行为封装起来的载体是类,类通常对客户隐藏其实现细节,这就是封装的思想.封装最主要的功能在于我们能修改自己的实现代码,而不用修改那些调用我们代码的 ...

  3. 数据库 基础篇3(mysql语法)

    4 数据库管理(接上篇) 4.1 查询所有数据库 mysql> show databases; +--------------------+ | Database           | +-- ...

  4. AngularJS: 'Template for directive must have exactly one root element' when using 'th' tag in directive template

    .controller('HomeController', function($scope,$location) { $scope.userName='天下大势,为我所控!'; $scope.clkU ...

  5. 学习swift开源项目

    如果你是位iOS开发者,或者你正想进入该行业,那么Swift为你提供了一个绝佳的机会.Swift的设计非常优雅,较Obj-C更易于学习,当然也非常强大. 为了指导开发者使用Swift进行开发,苹果发布 ...

  6. Positive-definite kernel

    Definition Let be a sequence of (complex) Hilbert spaces and be the bounded operators from Hi to Hj. ...

  7. 向mysql workbench中导入.sql文件

    mysql workbench用的不多,前段时间装了一下,然后用了一下,感觉操作比dbdesigner4要更人性化一点.其中二个方面做了改进,让我觉得很爽. 第一,就是端口可以修改了,以前就是定死33 ...

  8. 关于javascript中的===和==

    =是赋值符号,==是等于,===是严格等于. 对于等号两边的数值,如果类型不相同会先转换类型再比较,===则不会转换类型. 例如3和“3”在==比较下true,在===下是false, null和un ...

  9. CSS 选择器【详解】

    转自:http://www.cnblogs.com/polk6/archive/2013/07/19/3142142.html CSS 选择器及各样式引用方式介绍 一个好的界面,是一个Web吸引人们最 ...

  10. iOS学习之GCD

    多线程编程 线程定义:一个CPU执行的CPU命令 列一条无分叉的路径就叫线程. 多线程:执行多个不同的CPU命令 有多条路径. 线程的使用:主线程(又叫作UI线程)主要任务是处理UI事件,显示和刷新U ...