题意:有一张无向图,每条边有两个权值。求选取一些边使1和n连通,且max(a[i])+max(b[i])最小

2<=n<=50,000

0<=m<=100,000

1<=ai ,bi<=50,000

思路:LCT模板

将a[i]排序,维护路径上b[i]的最大值

因为是无向图且连通情况不变,不用findroot检查是否连通,并查集即可

也许是OI生涯最后一次打LCT

 var t:array[..,..]of longint;
rev,fa,mx,w,a,b,f,q,x,y:array[..]of longint;
n,m,i,j,u,v,ans,tmp,top:longint; procedure swap(var x,y:longint);
var t:longint;
begin
t:=x; x:=y; y:=t;
end; function min(x,y:longint):longint;
begin
if x<y then exit(x);
exit(y);
end; function isroot(x:longint):boolean;
begin
if (t[fa[x],]<>x)and(t[fa[x],]<>x) then exit(true);
exit(false);
end; procedure pushup(p:longint);
var l,r:longint;
begin
l:=t[p,]; r:=t[p,];
mx[p]:=p;
if w[mx[l]]>w[mx[p]] then mx[p]:=mx[l];
if w[mx[r]]>w[mx[p]] then mx[p]:=mx[r];
end; procedure pushdown(p:longint);
var l,r:longint;
begin
l:=t[p,]; r:=t[p,];
if rev[p]= then
begin
rev[p]:=; rev[l]:=rev[l] xor ; rev[r]:=rev[r] xor ;
swap(t[p,],t[p,]);
end;
end; procedure rotate(x:longint);
var y,z,l,r:longint;
begin
y:=fa[x]; z:=fa[y];
if t[y,]=x then l:=
else l:=;
r:=l xor ;
if not isroot(y) then
begin
if t[z,]=y then t[z,]:=x
else t[z,]:=x;
end;
fa[x]:=z; fa[y]:=x; fa[t[x,r]]:=y;
t[y,l]:=t[x,r]; t[x,r]:=y;
pushup(y);
pushup(x);
end; procedure splay(x:longint);
var y,z,k:longint;
begin
inc(top); q[top]:=x;
k:=x;
while not isroot(k) do
begin
inc(top); q[top]:=fa[k];
k:=fa[k];
end;
while top> do
begin
pushdown(q[top]);
dec(top);
end; while not isroot(x) do
begin
y:=fa[x]; z:=fa[y];
if not isroot(y) then
begin
if (t[y,]=x)xor(t[z,]=y) then rotate(x)
else rotate(y);
end;
rotate(x);
end;
end; procedure access(x:longint);
var k:longint;
begin
k:=;
while x> do
begin
splay(x); t[x,]:=k; pushup(x);
k:=x; x:=fa[x];
end;
end; procedure makeroot(x:longint);
begin
access(x); splay(x); rev[x]:=rev[x] xor ;
end; procedure link(x,y:longint);
begin
makeroot(x); fa[x]:=y;
end; procedure split(x,y:longint);
begin
makeroot(x); access(y); splay(y);
end; procedure cut(x,y:longint);
begin
makeroot(x); access(y); splay(y); t[y,]:=; fa[x]:=;
pushup(y);
end; function query(x,y:longint):longint;
begin
split(x,y);
exit(mx[y]);
end; procedure qsort(l,r:longint);
var i,j,mid:longint;
begin
i:=l; j:=r; mid:=a[(l+r)>>];
repeat
while mid>a[i] do inc(i);
while mid<a[j] do dec(j);
if i<=j then
begin
swap(a[i],a[j]);
swap(b[i],b[j]);
swap(x[i],x[j]);
swap(y[i],y[j]);
inc(i); dec(j);
end;
until i>j;
if l<j then qsort(l,j);
if i<r then qsort(i,r);
end; function find(k:longint):longint;
begin
if f[k]<>k then f[k]:=find(f[k]);
exit(f[k]);
end; begin
assign(input,'bzoj3669.in'); reset(input);
assign(output,'bzoj3669.out'); rewrite(output);
readln(n,m);
for i:= to n do f[i]:=i;
for i:= to m do read(x[i],y[i],a[i],b[i]);
qsort(,m);
ans:=maxlongint; for i:= to m do
begin
u:=x[i]; v:=y[i];
if find(u)=find(v) then
begin
tmp:=query(u,v);
if w[tmp]>b[i] then
begin
cut(tmp,x[tmp-n]);
cut(tmp,y[tmp-n]);
end
else
begin
if find()=find(n) then
begin
tmp:=query(,n);
ans:=min(ans,w[tmp]+a[i]);
end;
continue;
end;
end
else f[find(u)]:=find(v);
w[i+n]:=b[i]; mx[i+n]:=i+n;
link(u,i+n); link(v,i+n);
if find()=find(n) then
begin
tmp:=query(,n);
ans:=min(ans,w[tmp]+a[i]);
end;
end;
if ans=maxlongint then writeln(-)
else writeln(ans);
close(input);
close(output);
end.

最新文章

  1. 王宝强新片P2P风波持续发酵,互金真的前途未卜?
  2. openssl lhash 数据结构哈希表
  3. DSP中的段
  4. 自己封装的一个无限滚动 mark 待传
  5. (旧)子数涵数&#183;PS——换脸
  6. There is no Action mapped for action name XXX. - [unknown location]
  7. 【C语言】结构组成(函数、语句、注释)
  8. my dup2
  9. dba_dependencies查询结果视图
  10. OleDbCommand cmd.Parameters.AddWithValue 添加参数时需要按照存储过程参数的顺序加入
  11. 进制转换,杭电0j-2031
  12. nginx负载均衡简单配置
  13. 听翁恺老师mooc笔记(13)--类型定义和联合
  14. 我眼中的 Nginx(二):HTTP/2 dynamic table size update
  15. kettle使用笔记1--基本安装和使用
  16. hackbar增强版 &amp; 在Firefox上安装未通过验证的扩展
  17. linux环境下安装qt过程
  18. python-day73--django课上项目01
  19. selenium3 浏览器驱动下载及验证
  20. codevs 1191 数轴染色 区间更新加延迟标记

热门文章

  1. 416 Partition Equal Subset Sum 分割相同子集和
  2. 188 Best Time to Buy and Sell Stock IV 买卖股票的最佳时机 IV
  3. Coprime Conundrum 容斥原理
  4. C# KeepAlive的设置
  5. oracle 创建表
  6. VB6程序中NULL注意事项
  7. pthread Win32多线程编程的一些知识和感想
  8. C++ 类、对象、class
  9. Laravel Passport认证-多表、多字段解决方案
  10. xingo的demo部署