不难想到从小到大穷举a,判断在携带不超过a个B型精灵的情况下最少携带多少个B型精灵
这是一个经典的问题,显然要求出最小生成树,树上1到N路径上最大的边即是答案
所以我们要动态维护一个最小生成树可以用link cut tree维护
根据最小生成树的回路性质,我们加入一条边(u,v),保留树上u-v路径上最大边和新边中小的那个
由于这里维护的是边权,我们可以把每条边当做一个点p(u,v),连接(u,v)时,即是连接(u,p)和(v,p)
然后记录下路径上最大点权的编号即可

  1. var son:array[..,..] of longint;
  2. q,f,w,fa,v:array[..] of longint;
  3. rev:array[..] of boolean;
  4. s,e,a,b:array[..] of longint;
  5. ans,i,n,m:longint;
  6.  
  7. function min(a,b:longint):longint;
  8. begin
  9. if a>b then exit(b) else exit(a);
  10. end;
  11.  
  12. function getf(x:longint):longint;
  13. begin
  14. if f[x]<>x then f[x]:=getf(f[x]);
  15. exit(f[x]);
  16. end;
  17.  
  18. procedure swap(var a,b:longint);
  19. var c:longint;
  20. begin
  21. c:=a;
  22. a:=b;
  23. b:=c;
  24. end;
  25.  
  26. function root(x:longint):boolean;
  27. begin
  28. exit((son[fa[x],]<>x) and (son[fa[x],]<>x));
  29. end;
  30.  
  31. procedure update(x:longint);
  32. begin
  33. w[x]:=x;
  34. if v[w[son[x,]]]>v[w[x]] then w[x]:=w[son[x,]];
  35. if v[w[son[x,]]]>v[w[x]] then w[x]:=w[son[x,]];
  36. end;
  37.  
  38. procedure push(x:longint);
  39. begin
  40. if rev[x] then
  41. begin
  42. rev[son[x,]]:=not rev[son[x,]];
  43. rev[son[x,]]:=not rev[son[x,]];
  44. swap(son[x,],son[x,]);
  45. rev[x]:=false;
  46. end;
  47. end;
  48.  
  49. procedure rotate(x,w:longint);
  50. var y:longint;
  51. begin
  52. y:=fa[x];
  53. if not root(y) then
  54. begin
  55. if son[fa[y],]=y then son[fa[y],]:=x
  56. else son[fa[y],]:=x;
  57. end;
  58. fa[x]:=fa[y];
  59. son[y,-w]:=son[x,w];
  60. if son[x,w]<> then fa[son[x,w]]:=y;
  61. son[x,w]:=y;
  62. fa[y]:=x;
  63. update(y);
  64. end;
  65.  
  66. procedure splay(x:longint);
  67. var y,t,i:longint;
  68. begin
  69. t:=;
  70. i:=x;
  71. while not root(i) do
  72. begin
  73. inc(t);
  74. q[t]:=i;
  75. i:=fa[i];
  76. end;
  77. inc(t);
  78. q[t]:=i;
  79. for i:=t downto do
  80. push(q[i]);
  81. while not root(x) do
  82. begin
  83. y:=fa[x];
  84. if root(y) then
  85. begin
  86. if son[y,]=x then rotate(x,)
  87. else rotate(x,);
  88. end
  89. else begin
  90. if son[fa[y],]=y then
  91. begin
  92. if son[y,]=x then rotate(y,)
  93. else rotate(x,);
  94. rotate(x,);
  95. end
  96. else begin
  97. if son[y,]=x then rotate(x,)
  98. else rotate(y,);
  99. rotate(x,);
  100. end;
  101. end;
  102. end;
  103. update(x);
  104. end;
  105.  
  106. procedure access(x:longint);
  107. var y:longint;
  108. begin
  109. y:=;
  110. repeat
  111. splay(x);
  112. son[x,]:=y;
  113. update(x);
  114. y:=x;
  115. x:=fa[x];
  116. until x=;
  117. end;
  118.  
  119. procedure sort(l,r: longint);
  120. var i,j,x,y: longint;
  121. begin
  122. i:=l;
  123. j:=r;
  124. x:=a[(l+r) div ];
  125. repeat
  126. while a[i]<x do inc(i);
  127. while x<a[j] do dec(j);
  128. if not(i>j) then
  129. begin
  130. swap(s[i],s[j]);
  131. swap(e[i],e[j]);
  132. swap(a[i],a[j]);
  133. swap(b[i],b[j]);
  134. inc(i);
  135. j:=j-;
  136. end;
  137. until i>j;
  138. if l<j then sort(l,j);
  139. if i<r then sort(i,r);
  140. end;
  141.  
  142. procedure makeroot(x:longint);
  143. begin
  144. access(x);
  145. splay(x);
  146. rev[x]:=not rev[x];
  147. end;
  148.  
  149. procedure path(x,y:longint);
  150. begin
  151. makeroot(x);
  152. access(y);
  153. splay(y);
  154. end;
  155.  
  156. procedure link(x,y:longint);
  157. begin
  158. makeroot(x);
  159. fa[x]:=y;
  160. end;
  161.  
  162. procedure cut(x,y:longint);
  163. begin
  164. makeroot(x);
  165. access(y);
  166. splay(y);
  167. son[y,]:=;
  168. fa[x]:=;
  169. end;
  170.  
  171. procedure deal(i:longint);
  172. var x,y,z:longint;
  173. begin
  174. x:=getf(s[i]);
  175. y:=getf(e[i]);
  176. if x<>y then
  177. begin
  178. f[x]:=y;
  179. link(s[i],n+i);
  180. link(e[i],n+i);
  181. end
  182. else begin
  183. x:=s[i];
  184. y:=e[i];
  185. path(x,y);
  186. z:=w[y];
  187. if v[z]>b[i] then
  188. begin
  189. cut(s[z-n],z);
  190. cut(e[z-n],z);
  191. link(s[i],n+i);
  192. link(e[i],n+i);
  193. end;
  194. end;
  195. end;
  196.  
  197. begin
  198. readln(n,m);
  199. for i:= to m do
  200. readln(s[i],e[i],a[i],b[i]);
  201. for i:= to n do
  202. f[i]:=i;
  203. sort(,m);
  204. for i:= to m do
  205. begin
  206. v[n+i]:=b[i];
  207. w[n+i]:=n+i;
  208. end;
  209. ans:=;
  210. i:=;
  211. while i<m do
  212. begin
  213. inc(i);
  214. deal(i);
  215. while a[i]=a[i+] do
  216. begin
  217. inc(i);
  218. deal(i);
  219. end;
  220. if getf()=getf(n) then
  221. begin
  222. path(,n);
  223. ans:=min(ans,a[i]+v[w[n]]);
  224. end;
  225. end;
  226. if ans= then writeln(-)
  227. else writeln(ans);
  228. end.

bzoj3669的更多相关文章

  1. 【BZOJ3669】【Noi2014】魔法森林(Link-Cut Tree)

    [BZOJ3669][Noi2014]魔法森林(Link-Cut Tree) 题面 题目描述 为了得到书法大家的真传,小 E 同学下定决心去拜访住在魔法森林中的隐 士.魔法森林可以被看成一个包含 n ...

  2. 【BZOJ3669】【NOI2014】魔法森林 LCT

    题目描述 给你一个\(n\)个点\(m\)条边的图,每条边有两个边权\(a,b\).请你找出从\(1\)到\(n\)一条路径,使得这条路径上边权\(a\)的最大值\(+\)边权\(b\)的最大值最小. ...

  3. bzoj3669: [Noi2014]魔法森林 lct版

    先上题目 bzoj3669: [Noi2014]魔法森林 这道题首先每一条边都有一个a,b 我们按a从小到大排序 每次将一条路劲入队 当然这道题权在边上 所以我们将边化为点去连接他的两个端点 当然某两 ...

  4. 【BZOJ3669】魔法森林(LCT)

    题意:有一张无向图,每条边有两个权值.求选取一些边使1和n连通,且max(a[i])+max(b[i])最小 2<=n<=50,000 0<=m<=100,000 1<= ...

  5. [bzoj3669][Noi2014]魔法森林_LCT_并查集

    魔法森林 bzoj-3669 Noi-2014 题目大意:说不明白题意系列++……题目链接 注释:略. 想法:如果只有1个参量的话spfa.dij什么的都上来了. 两个参量的话我们考虑,想将所有的边按 ...

  6. 【BZOJ3669】[Noi2014]魔法森林 LCT

    终于不是裸的LCT了...然而一开始一眼看上去这是kruskal..不对,题目要求1->n的路径上的每个点的两个最大权值和最小,这样便可以用LCT来维护一个最小生成路(瞎编的...),先以a为关 ...

  7. BZOJ3669 (动态树)

    Problem 魔法森林 (NOI2014) 题目大意 给n个点,m条边的无向图,每条边有两个权值a,b. 求一条从1-->n的路径,使得这条路径上max(a)+max(b)最小.输出最小值即可 ...

  8. bzoj3669[Noi2014]魔法森林

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  9. BZOJ-3669 魔法森林 Link-Cut-Tree

    意识到背模版的重要性了,记住了原理和操作,然后手打模版残了..颓我时间...... 3669: [Noi2014]魔法森林 Time Limit: 30 Sec Memory Limit: 512 M ...

随机推荐

  1. mark jquery 链式调用的js原理

    我们在使用jquery的时候会用到类似$("#id").css('color','red').show(200); 这样写有点减少代码量,减少了逐步查询DOM的性能损耗: js 原 ...

  2. JavaWeb_Day10_学习笔记1_response(3、4、5、6、7、8、9)发送状态码、响应、重定向、定时刷新、禁用浏览器缓存、响应字节数据、快捷重定向方法、完成防盗链

    今天学习重点: 1.response和request响应和应答分别学习: 请求响应流程图 response 1        response概述 response是Servlet.service方法 ...

  3. 安装oracle pl/sql developer

    1.在官网上下载oracle 11g R2版本的数据库,直接常规安装.数据库可以下载32bit. 2.在这里下载oracle client (32bit)http://www.oracle.com/t ...

  4. 什么是NSTimer

    本文主要是介绍什么是NSTimer,具体使用请参考上一篇博客. 1.什么是NSTimer? NSTimer就是timer就是一个能在从现在开始的后面的某一个时刻或者周期性的执行我们指定的方法的对象. ...

  5. Solr集群的搭建以及使用(内涵zookeeper集群的搭建指南)

    1   什么是SolrCloud SolrCloud(solr 云)是Solr提供的分布式搜索方案,当你需要大规模,容错,分布式索引和检索能力时使用 SolrCloud.当一个系统的索引数据量少的时候 ...

  6. 日期字符串转换为NSDate

    // 纯数字日期 NSString *str1 = "; // 日期字符串 NSString *str2 = @"2015/05/12 10:22:01"; // 带时区 ...

  7. ABP手机端调用API时的CORS

    这个问题其实很早就考虑了,当时因为也没有特别着急去解决这个问题,就一直拖着.... 好吧,拖延症是不好的,所有不懒得做的,终将会逼着你去再很短的时间内去解决问题...实现项目 改写一个已有的webfo ...

  8. spark-shell - 将结果保存成一个文件

    sqlContext.sql("""    SELECT user_no,cust_id,oper_code     FROM cui.operation_data_an ...

  9. BFC与hasLayout之间的故事

    刚拒绝了一个很有诱惑的公司,不是不想去,而是对现在的能力还不确定,希望能够进一步提高自己的技能,所有想写博客了,监督自己的学习进度·········现在还没有开放博客,希望成熟一些后再开放吧! 进入正 ...

  10. css实现的透明三角形

    css实现下图样式,具体像素值记不住了,很好设置,html code (2014百度秋招面试题): <div id="demo"></div>   分析:这 ...