bzoj3669
不难想到从小到大穷举a,判断在携带不超过a个B型精灵的情况下最少携带多少个B型精灵
这是一个经典的问题,显然要求出最小生成树,树上1到N路径上最大的边即是答案
所以我们要动态维护一个最小生成树可以用link cut tree维护
根据最小生成树的回路性质,我们加入一条边(u,v),保留树上u-v路径上最大边和新边中小的那个
由于这里维护的是边权,我们可以把每条边当做一个点p(u,v),连接(u,v)时,即是连接(u,p)和(v,p)
然后记录下路径上最大点权的编号即可
var son:array[..,..] of longint;
q,f,w,fa,v:array[..] of longint;
rev:array[..] of boolean;
s,e,a,b:array[..] of longint;
ans,i,n,m:longint; function min(a,b:longint):longint;
begin
if a>b then exit(b) else exit(a);
end; function getf(x:longint):longint;
begin
if f[x]<>x then f[x]:=getf(f[x]);
exit(f[x]);
end; procedure swap(var a,b:longint);
var c:longint;
begin
c:=a;
a:=b;
b:=c;
end; function root(x:longint):boolean;
begin
exit((son[fa[x],]<>x) and (son[fa[x],]<>x));
end; procedure update(x:longint);
begin
w[x]:=x;
if v[w[son[x,]]]>v[w[x]] then w[x]:=w[son[x,]];
if v[w[son[x,]]]>v[w[x]] then w[x]:=w[son[x,]];
end; procedure push(x:longint);
begin
if rev[x] then
begin
rev[son[x,]]:=not rev[son[x,]];
rev[son[x,]]:=not rev[son[x,]];
swap(son[x,],son[x,]);
rev[x]:=false;
end;
end; procedure rotate(x,w:longint);
var y:longint;
begin
y:=fa[x];
if not root(y) then
begin
if son[fa[y],]=y then son[fa[y],]:=x
else son[fa[y],]:=x;
end;
fa[x]:=fa[y];
son[y,-w]:=son[x,w];
if son[x,w]<> then fa[son[x,w]]:=y;
son[x,w]:=y;
fa[y]:=x;
update(y);
end; procedure splay(x:longint);
var y,t,i:longint;
begin
t:=;
i:=x;
while not root(i) do
begin
inc(t);
q[t]:=i;
i:=fa[i];
end;
inc(t);
q[t]:=i;
for i:=t downto do
push(q[i]);
while not root(x) do
begin
y:=fa[x];
if root(y) then
begin
if son[y,]=x then rotate(x,)
else rotate(x,);
end
else begin
if son[fa[y],]=y then
begin
if son[y,]=x then rotate(y,)
else rotate(x,);
rotate(x,);
end
else begin
if son[y,]=x then rotate(x,)
else rotate(y,);
rotate(x,);
end;
end;
end;
update(x);
end; procedure access(x:longint);
var y:longint;
begin
y:=;
repeat
splay(x);
son[x,]:=y;
update(x);
y:=x;
x:=fa[x];
until x=;
end; procedure sort(l,r: longint);
var i,j,x,y: longint;
begin
i:=l;
j:=r;
x:=a[(l+r) div ];
repeat
while a[i]<x do inc(i);
while x<a[j] do dec(j);
if not(i>j) then
begin
swap(s[i],s[j]);
swap(e[i],e[j]);
swap(a[i],a[j]);
swap(b[i],b[j]);
inc(i);
j:=j-;
end;
until i>j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end; procedure makeroot(x:longint);
begin
access(x);
splay(x);
rev[x]:=not rev[x];
end; procedure path(x,y:longint);
begin
makeroot(x);
access(y);
splay(y);
end; procedure link(x,y:longint);
begin
makeroot(x);
fa[x]:=y;
end; procedure cut(x,y:longint);
begin
makeroot(x);
access(y);
splay(y);
son[y,]:=;
fa[x]:=;
end; procedure deal(i:longint);
var x,y,z:longint;
begin
x:=getf(s[i]);
y:=getf(e[i]);
if x<>y then
begin
f[x]:=y;
link(s[i],n+i);
link(e[i],n+i);
end
else begin
x:=s[i];
y:=e[i];
path(x,y);
z:=w[y];
if v[z]>b[i] then
begin
cut(s[z-n],z);
cut(e[z-n],z);
link(s[i],n+i);
link(e[i],n+i);
end;
end;
end; begin
readln(n,m);
for i:= to m do
readln(s[i],e[i],a[i],b[i]);
for i:= to n do
f[i]:=i;
sort(,m);
for i:= to m do
begin
v[n+i]:=b[i];
w[n+i]:=n+i;
end;
ans:=;
i:=;
while i<m do
begin
inc(i);
deal(i);
while a[i]=a[i+] do
begin
inc(i);
deal(i);
end;
if getf()=getf(n) then
begin
path(,n);
ans:=min(ans,a[i]+v[w[n]]);
end;
end;
if ans= then writeln(-)
else writeln(ans);
end.
bzoj3669的更多相关文章
- 【BZOJ3669】【Noi2014】魔法森林(Link-Cut Tree)
[BZOJ3669][Noi2014]魔法森林(Link-Cut Tree) 题面 题目描述 为了得到书法大家的真传,小 E 同学下定决心去拜访住在魔法森林中的隐 士.魔法森林可以被看成一个包含 n ...
- 【BZOJ3669】【NOI2014】魔法森林 LCT
题目描述 给你一个\(n\)个点\(m\)条边的图,每条边有两个边权\(a,b\).请你找出从\(1\)到\(n\)一条路径,使得这条路径上边权\(a\)的最大值\(+\)边权\(b\)的最大值最小. ...
- bzoj3669: [Noi2014]魔法森林 lct版
先上题目 bzoj3669: [Noi2014]魔法森林 这道题首先每一条边都有一个a,b 我们按a从小到大排序 每次将一条路劲入队 当然这道题权在边上 所以我们将边化为点去连接他的两个端点 当然某两 ...
- 【BZOJ3669】魔法森林(LCT)
题意:有一张无向图,每条边有两个权值.求选取一些边使1和n连通,且max(a[i])+max(b[i])最小 2<=n<=50,000 0<=m<=100,000 1<= ...
- [bzoj3669][Noi2014]魔法森林_LCT_并查集
魔法森林 bzoj-3669 Noi-2014 题目大意:说不明白题意系列++……题目链接 注释:略. 想法:如果只有1个参量的话spfa.dij什么的都上来了. 两个参量的话我们考虑,想将所有的边按 ...
- 【BZOJ3669】[Noi2014]魔法森林 LCT
终于不是裸的LCT了...然而一开始一眼看上去这是kruskal..不对,题目要求1->n的路径上的每个点的两个最大权值和最小,这样便可以用LCT来维护一个最小生成路(瞎编的...),先以a为关 ...
- BZOJ3669 (动态树)
Problem 魔法森林 (NOI2014) 题目大意 给n个点,m条边的无向图,每条边有两个权值a,b. 求一条从1-->n的路径,使得这条路径上max(a)+max(b)最小.输出最小值即可 ...
- bzoj3669[Noi2014]魔法森林
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- BZOJ-3669 魔法森林 Link-Cut-Tree
意识到背模版的重要性了,记住了原理和操作,然后手打模版残了..颓我时间...... 3669: [Noi2014]魔法森林 Time Limit: 30 Sec Memory Limit: 512 M ...
随机推荐
- XMLHttpRequest cannot load的问题解决方法
在chrome中可以用--allow-file-access-from-files 命令来解决这个问题.右键点击chrome的快捷方式选择属性.在目标一栏中添加--allow-file-acces ...
- mysql分表,分区的区别和联系
一,什么是mysql分表,分区 什么是分表,从表面意思上看呢,就是把一张表分成N多个小表,具体请看mysql分表的3种方法 什么是分区,分区呢就是把一张表的数据分成N多个区块,这些区块可以在同一个磁盘 ...
- phaser源码解析(二) Phaser.Utils类下pad方法
/** *#填充字符串方法 * Javascript string pad http://www.webtoolkit.info/. * pad = the string to pad it out ...
- javascript格式化指定的日期对象
/* * 格式化Date对象为:“2015-04-17 10:20:00” * var dateObj = new Date(); */ function formartDate(dateObj){ ...
- 关于SringMvc的参数的传递
* @RequestMapping这个注解代表要请求的方法 * value值表示请求的 方法名*********@RequestParam(value="username")代表请 ...
- OI路上-NOIP100天冲刺计划
学OI已经9个月了,可是自己水平还是那样的弱QWQ. 现在离NOIP还有差不多100天的时间. 晚上辗转反侧发现了自己的一些问题: (1)DP还经常没思路. (2)搜索恶心题还不想写. (3)有时候也 ...
- C++ map插入(insert)数据返回值
例子: typedef boost::unordered_map<int, int> UserOnlineMap; UserOnlineMap userOnlineMap_; std::p ...
- zlib压缩解压示例
#include <stdio.h> #include <string.h> #include <assert.h> #include "zlib.h&q ...
- 九度0J 1374 所有员工年龄排序
题目地址:http://ac.jobdu.com/problem.php?pid=1374 题目描述: 公司现在要对所有员工的年龄进行排序,因为公司员工的人数非常多,所以要求排序算法的效率要非常高,你 ...
- 九度OJ 1451 不容易系列之一 -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1451 题目描述: 大家常常感慨,要做好一件事情真的不容易,确实,失败比成功容易多了! 做好“一件”事情尚且不易,若 ...