「luogu2387」[NOI2014] 魔法森林

题目大意

\(n\) 个点 \(m\) 条边的无向图,每条边上有两个权值 \(a,b\),求从 \(1\) 节点到 \(n\) 节点 \(max\{a\}+max\{b\}\) 的最小值。图中可能有重边和自环。\((n \leq 5 \times 10^4 , m \leq 10^5)\)

一句话题解

考虑生成树 ( 过程类似 \(kruskal​\) );

把边按照 \(a\) 从小到大排序,\(1-m\) 枚举边,设边连接的两点为 \(u , v\);

若 \(u , v\) 已经在一个联通块中,则在 \(u\) 到 \(v\) 的路径中找一个 \(b\) 的最大值 \(b_{max}\),如果 \(b_{max} > b当前枚举到的边\),就把 \(当前枚举到的边\) 连接,\(max_b\) 所在的边断开;

若 \(u , v\) 不在一个联通块中,则直接连接;

边用 \(lct\) 维护即可;

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
inline int in() {
int x=0;char c=getchar();bool f=false;
while(c<'0'||c>'9') f|=c=='-', c=getchar();
while(c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48), c=getchar();
return f?-x:x;
} const int N = 5e4+5, M = 1e5+5;
struct edge {
int u, v, a, b;
friend inline bool operator < (const edge &x, const edge &y) {
return x.a < y.a;
}
}e[M];
int n, m; template<typename T>inline void chk_min(T &_, T __) { _=_<__?_:__; } struct link_cut_tree {
#define lson c[p][0]
#define rson c[p][1]
int c[N+M][2], fa[N+M], val[N+M], max[N+M];
bool rev[N+M]; inline bool nroot(int p) {
return c[fa[p]][0]==p||c[fa[p]][1]==p;
}
inline void push_up(int p) {
max[p]=p;
if(lson&&val[max[lson]]>val[max[p]]) max[p]=max[lson];
if(rson&&val[max[rson]]>val[max[p]]) max[p]=max[rson];
}
inline void rever(int p) {
std::swap(lson, rson);
rev[p]^=1;
}
inline void push_down(int p) {
if(rev[p]) {
if(lson) rever(lson);
if(rson) rever(rson);
rev[p]=false;
}
}
void push_all(int p) {
if(nroot(p)) push_all(fa[p]);
push_down(p);
}
inline void rotate(int x) {
int y=fa[x], z=fa[y], l=c[y][1]==x, r=l^1;
if(nroot(y)) c[z][c[z][1]==y]=x;
fa[x]=z, fa[c[x][r]]=y, fa[y]=x;
c[y][l]=c[x][r], c[x][r]=y;
push_up(y), push_up(x);
}
inline void splay(int x) {
int y, z;
push_all(x);
while(nroot(x)) {
y=fa[x], z=fa[y];
if(nroot(y)) rotate(c[y][0]==x^c[z][0]==y?x:y);
rotate(x);
}
}
inline void access(int p) { for(int s=0;p;p=fa[s=p]) splay(p), rson=s, push_up(p); }
inline void make_root(int p) { access(p), splay(p), rever(p); }
inline void split(int p, int s) { make_root(p), access(s), splay(s); }
inline void link(int p, int s) { make_root(p), fa[p]=s; }
inline void cut(int p, int s) { split(p, s), c[s][0]=fa[p]=0, push_up(s); }
inline int select(int p, int s) { split(p, s); return max[s]; }
#undef lson
#undef rson
}lct; struct disjoint_set_union {
int fa[N];
int get_fa(int u) { return u==fa[u]?u:fa[u]=get_fa(fa[u]); }
inline int & operator [] (int u) { return fa[u]; }
}dsu; inline int kruskal() {
int ret=-1;
std::sort(e+1, e+1+m);
for(int i=1;i<=n;++i) dsu[i]=i;
for(int i=n+1;i<=n+m;++i) lct.val[i]=e[i-n].b;
for(int i=1;i<=n+m;++i) lct.max[i]=i;
for(int i=1;i<=m;++i) {
if(e[i].u==e[i].v) continue;
int u=dsu.get_fa(e[i].u), v=dsu.get_fa(e[i].v);
if(u==v) {
int k=lct.select(e[i].u, e[i].v);
if(lct.val[k]>e[i].b) {
lct.cut(e[k-n].u, k), lct.cut(k, e[k-n].v);
lct.link(e[i].u, i+n), lct.link(i+n, e[i].v);
}
}
else {
dsu[u]=v;
lct.link(e[i].u, i+n), lct.link(i+n, e[i].v);
}
if(dsu.get_fa(1)==dsu.get_fa(n)) {
lct.split(1, n);
if(ret==-1) ret=lct.val[lct.max[n]]+e[i].a;
else chk_min(ret, lct.val[lct.max[n]]+e[i].a);
}
}
return ret;
} int main() {
n=in(), m=in();
for(int i=1;i<=m;++i)
e[i]=(edge){in(), in(), in(), in()};
printf("%d\n", kruskal());
return 0;
}

「luogu2387」[NOI2014] 魔法森林的更多相关文章

  1. NOI2014 魔法森林

    3669: [Noi2014]魔法森林 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 106  Solved: 62[Submit][Status] ...

  2. bzoj 3669: [Noi2014]魔法森林 动态树

    3669: [Noi2014]魔法森林 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 363  Solved: 202[Submit][Status] ...

  3. BZOJ 3669: [Noi2014]魔法森林( LCT )

    排序搞掉一维, 然后就用LCT维护加边MST. O(NlogN) ------------------------------------------------------------------- ...

  4. bzoj 3669: [Noi2014]魔法森林

    bzoj 3669: [Noi2014]魔法森林 Description 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看成一个包含个N节点M条边的无向图,节点标号 ...

  5. BZOJ_3669_[Noi2014]魔法森林_LCT

    BZOJ_3669_[Noi2014]魔法森林_LCT Description 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看成一个包含个N节点M条边的无向图,节 ...

  6. bzoj 3669: [Noi2014]魔法森林 (LCT)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3669 题面: 3669: [Noi2014]魔法森林 Time Limit: 30 Sec  ...

  7. P2387 [NOI2014]魔法森林(LCT)

    P2387 [NOI2014]魔法森林 LCT边权维护经典题 咋维护呢?边化为点,边权变点权. 本题中我们把边对关键字A进行排序,动态维护关键字B的最小生成树 加边后出现环咋办? splay维护最大边 ...

  8. [NOI2014]魔法森林 LCT

    题面 [NOI2014]魔法森林 题解 一条路径的代价为路径上的\(max(a[i]) + max(b[i])\),因为一条边同时有$a[i], b[i]$2种权值,直接处理不好同时兼顾到,所以我们考 ...

  9. bzoj 3669: [Noi2014]魔法森林 -- 动点spfa

    3669: [Noi2014]魔法森林 Time Limit: 30 Sec  Memory Limit: 512 MB 动点spfa Description 为了得到书法大家的真传,小E同学下定决心 ...

随机推荐

  1. 手写token解析器、语法解析器、LLVM IR生成器(GO语言)

    最近开始尝试用go写点东西,正好在看LLVM的资料,就写了点相关的内容 - 前端解析器+中间代码生成(本地代码的汇编.执行则靠LLVM工具链完成) https://github.com/daibinh ...

  2. Linux操作系统的文件链接

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++标题:Linux操作系统的文件链接内容:文件链接时间:2019年 ...

  3. Python之常用第三方库总结

    在使用python进行开发的时候,经常我们需要借助一些第三方库,进行日常代码的开发工作.这里总结一些常用的类库 1. requests Requests 是用Python语言编写,基于 urllib, ...

  4. css 3 新特性

    CSS3的新特性大致分为以下六类 1.CSS3选择器 2.CSS3边框与圆角 3.CSS3背景与渐变 4.CSS3过渡 5.CSS3变换 6.CSS3动画 下面分别说一说以上六类都有哪些内容 CSS3 ...

  5. Python3 视频教程,全网最全的视频教程,爬虫,从入门到实战

    需要联系我:QQ:1844912514 最新Python基础班+就业班视频教程 链接: python分布式爬虫打造搜索引擎链接: https://pan.baidu.com/s/1N7HL7U0gQX ...

  6. Django(三) ORM 数据库操作

    大纲 一.DjangoORM 创建基本类型及生成数据库表结构 1.简介 2.创建数据库 表结构 二.Django ORM基本增删改查 1.表数据增删改查 2.表结构修改 三.Django ORM 字段 ...

  7. Supervisor安装与使用

    一.简介 1.supervisor是什么 superviosr是一个Linux/Unix系统上进程监控和管理的工具,它由python编写,可以用pip安装.supervisor能将一个普通的命令行进程 ...

  8. codeforces131D

    Subway CodeForces - 131D A subway scheme, classic for all Berland cities is represented by a set of  ...

  9. 本机Jenkins的使用

    1.启动jenkins: 命令:java -jar D:\toolspackage\jenkins\jenkins.war  打开jenkins网页:http://localhost:8080/ 2. ...

  10. v-for 在 VSCode 中出现 Elements in iteration expect to have 'v-bind:key' directives.

    在 VSCode 中编辑代码时,在有 v-for 的语句下面有一条红色波浪线,鼠标放上去有提示 Elements in iteration expect to have 'v-bind:key' di ...