「luogu2387」[NOI2014] 魔法森林
「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] 魔法森林的更多相关文章
- NOI2014 魔法森林
3669: [Noi2014]魔法森林 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 106 Solved: 62[Submit][Status] ...
- bzoj 3669: [Noi2014]魔法森林 动态树
3669: [Noi2014]魔法森林 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 363 Solved: 202[Submit][Status] ...
- BZOJ 3669: [Noi2014]魔法森林( LCT )
排序搞掉一维, 然后就用LCT维护加边MST. O(NlogN) ------------------------------------------------------------------- ...
- bzoj 3669: [Noi2014]魔法森林
bzoj 3669: [Noi2014]魔法森林 Description 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看成一个包含个N节点M条边的无向图,节点标号 ...
- BZOJ_3669_[Noi2014]魔法森林_LCT
BZOJ_3669_[Noi2014]魔法森林_LCT Description 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看成一个包含个N节点M条边的无向图,节 ...
- bzoj 3669: [Noi2014]魔法森林 (LCT)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3669 题面: 3669: [Noi2014]魔法森林 Time Limit: 30 Sec ...
- P2387 [NOI2014]魔法森林(LCT)
P2387 [NOI2014]魔法森林 LCT边权维护经典题 咋维护呢?边化为点,边权变点权. 本题中我们把边对关键字A进行排序,动态维护关键字B的最小生成树 加边后出现环咋办? splay维护最大边 ...
- [NOI2014]魔法森林 LCT
题面 [NOI2014]魔法森林 题解 一条路径的代价为路径上的\(max(a[i]) + max(b[i])\),因为一条边同时有$a[i], b[i]$2种权值,直接处理不好同时兼顾到,所以我们考 ...
- bzoj 3669: [Noi2014]魔法森林 -- 动点spfa
3669: [Noi2014]魔法森林 Time Limit: 30 Sec Memory Limit: 512 MB 动点spfa Description 为了得到书法大家的真传,小E同学下定决心 ...
随机推荐
- FLOAT 和 DOUBLE区别
以下是 FLOAT 和 DOUBLE 的区别: float : 单精度浮点数 double : 双精度浮点数 ·浮点数以 8 位精度存储在 FLOAT 中,并且有四个字节. ·浮点数存储在 DOUBL ...
- mycat入门--数据库分片
配置mycat的用户名和密码: 连接mycat,就像连接mysql一样:
- html中滚动小球的demo
类似于下图的效果: 代码: <!DOCTYPE html> <html> <head> <title>Promise animation</tit ...
- MVC中使用Hangfire按秒执行任务
更新Hangfire版本到1.7.0,才支持使用按秒循环任务执行 RecurringJob.AddOrUpdate("test",()=>writeLog("每20 ...
- ElasticSearch搜索(一)
首先从ES的支持的字段说起,ES文档中字段有多种类型 官方文档. 这几个比较常用: text,keyword,integer,float,boolean,object,geo_point(地理坐标), ...
- jenkins针对不同的项目组对用户进行权限分配
因jenkins上存有de(开发).te(测试)等三个不同环境的项目,同时因为项目需求,需要对不同的开发及测试人员配置不同的jenkins权限,即以项目为单位,对不同人员进行不同权限配置,要求如下: ...
- python调用openstack的api,create_instance的程序解析
python调用openstack的api,create_instance的程序解析 2017年10月17日 15:27:24 CloudXli 阅读数:848 版权声明:本文为博主原创文章,未经 ...
- VS2010查看源码对应的汇编语言
在学习c++中const关键字的过程中,经常会看到各种寄存器.汇编指令分析,像下面的图这样 左图是g++中反汇编的效果,右图是vs中反汇编的效果. 如果我们想要查看源码所对应的汇编语言,应该怎么操作呢 ...
- linux网络性能测试工具ipref安装与使用
一.iperf工具安装 源码包下载地址:https://iperf.fr/iperf-download.php#archlinux 选择对应系统的版本就是解压安装了 完成 测试发现有问题 问题原因:L ...
- sql笔试题
笔试题1: 1.select * from tablex where name = "张*" order by age 默认升序 select * from table ...