将边按 排序,然后枚举 a 值,在低于 枚举值的边都加入到 LCT 中,若 加入改变不会形成环,则直接加入这条边,反之 若替换掉路径上 b 最大的边可以得到更优解(即 b < ),则替换掉路径上 b 最大的边,当 连通时更新答案。
写的时候只要将边按 作为关键字排序,依次加入每条边并更新答案。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 10;
typedef long long ll;
#define pii pair<int,int>
#define fir first
#define sec second
int n,m,ans;
struct node {
int u,v,a,b;
bool operator < (const node &rhs) const {
return a < rhs.a;
}
}E[maxn];
multiset<int> st;
inline int read(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
}
struct LCT { //用splay维护原森林的连通,用到了splay的操作以及数组
int ch[maxn][2]; //ch[u][0] 表示 左二子,ch[u][1] 表示右儿子
int f[maxn]; //当前节点的父节点
int tag[maxn]; //翻转标记,乘标记,加标记
int top,sta[maxn],sz[maxn];
int a[maxn],b[maxn],ma[maxn],mb[maxn];
inline bool get(int x) {
return ch[f[x]][1] == x;
}
void init() {
for (int i = 1; i <= n; i++)
sz[i] = 1, ma[i] = mb[i] = i, a[i] = b[i] = -2147483647;
}
inline void pushup(int rt) {
if (rt) {
sz[rt] = 1; ma[rt] = mb[rt] = rt;
int ls = ch[rt][0], rs = ch[rt][1];
if (ls) {
sz[rt] += sz[ls];
if (a[ma[ls]] > a[ma[rt]]) ma[rt] = ma[ls];
if (b[mb[ls]] > b[mb[rt]]) mb[rt] = mb[ls];
}
if (rs) {
sz[rt] += sz[rs];
if (a[ma[rs]] > a[ma[rt]]) ma[rt] = ma[rs];
if (b[mb[rs]] > b[mb[rt]]) mb[rt] = mb[rs];
}
}
}
inline void pushdown(int rt) {
if (tag[rt]) {
int ls = ch[rt][0], rs = ch[rt][1];
if (ls) swap(ch[ls][0],ch[ls][1]), tag[ls] ^= 1;
if (rs) swap(ch[rs][0],ch[rs][1]), tag[rs] ^= 1;
tag[rt] = 0;
}
}
inline bool isroot(int x) {
return (ch[f[x]][0] != x) && (ch[f[x]][1] != x);
}
inline void rotate(int x) { //旋转操作,根据 x 在 f[x] 的哪一侧进行左旋和右旋
int old = f[x], oldf = f[old];
int whichx = get(x);
if(!isroot(old)) ch[oldf][ch[oldf][1] == old] = x; //如果 old 不是根节点,就要修改 oldf 的子节点信息
ch[old][whichx] = ch[x][whichx ^ 1];
ch[x][whichx ^ 1] = old;
f[ch[old][whichx]] = old;
f[old] = x; f[x] = oldf;
pushup(old); pushup(x);
}
inline void splay(int x) { //将 x 旋到所在 splay 的根
top = 0; sta[++top] = x;
for (int i = x; !isroot(i); i = f[i]) sta[++top] = f[i]; //在 splay 中维护 下推标记
while(top) pushdown(sta[top--]);
for(int fa = f[x]; !isroot(x); rotate(x), fa = f[x]) { //再把x翻上来
if(!isroot(fa)) //如果fa非根,且x 和 fa是同一侧,那么先翻转fa,否则先翻转x
rotate((get(x) == get(fa)) ? fa : x);
}
}
inline void access(int x) { //access操作将x 到 根路径上的边修改为重边
int lst = 0;
while(x > 0) {
splay(x);
ch[x][1] = lst;
pushup(x);
lst = x; x = f[x];
}
}
inline void move_to_root(int x) { //将 x 移到 x 所在树的根(不是所在splay的根,所在splay只是一条重链)
access(x); splay(x); tag[x] ^= 1; swap(ch[x][0],ch[x][1]);
//将 x 移到 根之后 x 是深度最低的点,这条重链、这棵splay上所有点的深度颠倒,
//所有的点的左子树的点应该到右子树,因此要翻转这棵splay的左右子树
}
inline int findroot(int x) {
access(x);
splay(x);
int rt = x;
while(ch[rt][0]) rt = ch[rt][0];
return rt;
}
inline void split(int x,int y) {
move_to_root(x); access(y); splay(y);
}
inline void link(int x,int y) {
move_to_root(x); f[x] = y; splay(x);
}
inline void cut(int x,int y) {
split(x,y);
ch[y][0] = f[x] = 0;
pushup(y);
}
}tree;
int p[maxn];
int find(int x) {
return x == p[x] ? x : p[x] = find(p[x]);
}
int id,x,y,u,v,k[maxn],tot,vis[maxn];
char op[10];
int main() {
n = read(); m = read();
tree.init();
ans = 2147483647;
for (int i = 1; i <= m; i++) {
scanf("%d%d%d%d",&E[i].u,&E[i].v,&E[i].a,&E[i].b);
}
sort(E + 1,E + m + 1);
for (int i = 1; i <= m; i++) {
int u = E[i].u, v = E[i].v, a = E[i].a, b = E[i].b;
tree.a[i + n] = a, tree.b[i + n] = b;
tree.ma[i + n] = tree.mb[i + n] = i + n;
tree.sz[i + n] = 1;
if (u == v) continue;
if (tree.findroot(u) == tree.findroot(v)) {
tree.split(u,v);
int p = tree.mb[v];
if (b < tree.b[p]) {
tree.cut(E[p - n].u,p);
tree.cut(E[p - n].v,p);
tree.link(u,i + n);
tree.link(v,i + n);
}
} else {
tree.link(u,i + n);
tree.link(v,i + n);
}
if (tree.findroot(1) == tree.findroot(n)) {
tree.split(1,n);
ans = min(ans,tree.a[tree.ma[n]] + tree.b[tree.mb[n]]);
}
}
printf("%d\n",ans == 2147483647 ? -1 : ans);
return 0;
}