http://www.lydsy.com/JudgeOnline/problem.php?id=3669
首先看到题目应该可以得到我们要最小化
min{ max{a(u, v)} + max{b(u, v)} }
两个变量不好做。。。那么我们约束一个a
即按a从小到大排序,依次加边。
发现当有环出现时,去掉的是环中b最大的边。
证明:因为a是从小到大排序,因此此时答案为 a+max{b(u, v)},显然b越小越好。
然后需要link和cut操作。。。
脑洞开到这里开不动了。。。。。。。。。。。。。。。。。。。。。。。
从来没写过维护边权的lct啊啊啊啊啊啊啊啊。。。。!!!
十分犹豫后。。。看题解。。。。。。。。。。。。
QAQ
我怎么没想到将边看做一个单独的节点。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
说明我是sb。
那么本题就是将边看做一个单独的节点,然后lct即可。。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }
const int oo=~0u>>1, N=50005, M=100005;
int w[M];
struct node *null;
struct node {
node *f, *c[2];
int pos, k, rev;
node(int _p=0) { pos=k=_p; f=c[0]=c[1]=null; }
void setc(node *x, bool d) { c[d]=x; x->f=this; }
bool d() { return f->c[1]==this; }
bool check() { return f==null || (f->c[0]!=this && f->c[1]!=this); }
void upd() { if(this==null) return; rev=!rev; swap(c[0], c[1]); }
void pushup() { pos=k; if(w[c[0]->pos]>w[pos]) pos=c[0]->pos; if(w[c[1]->pos]>w[pos]) pos=c[1]->pos; }
void pushdown() { if(rev) rev=0, c[0]->upd(), c[1]->upd(); }
};
void rot(node *x) {
node *f=x->f;
f->pushdown(); x->pushdown(); bool d=x->d();
if(f->check()) x->f=f->f;
else f->f->setc(x, f->d());
f->setc(x->c[!d], d);
x->setc(f, !d);
f->pushup();
}
void fix(node *x) { if(!x->check()) fix(x->f); x->pushdown(); }
void splay(node *x) {
fix(x);
while(!x->check())
if(x->f->check()) rot(x);
else x->d()==x->f->d()?(rot(x->f), rot(x)):(rot(x), rot(x));
x->pushup();
}
node *access(node *x) {
node *y=null;
for(; x!=null; y=x, x=x->f) splay(x), x->c[1]=y;
return y;
}
void mkroot(node *x) { access(x)->upd(); splay(x); }
void split(node *x, node *y) { mkroot(x); access(y); splay(y); }
void link(node *x, node *y) { mkroot(x); x->f=y; }
void cut(node *x, node *y) { split(x, y); y->c[0]->f=null; y->c[0]=null; }
node *findrt(node *x) { access(x); splay(x); while(x->c[0]!=null) x=x->c[0]; return x; }
int ask(node *x, node *y) { split(x, y); return y->pos; }
struct dat { int u, v, a, b; }e[M];
node *root[N+M];
int n, m, ans=oo;
bool cmp(const dat &a, const dat &b) { return a.a<b.a; }
void init() {
w[0]=-oo;
sort(e+1, e+1+m, cmp);
for1(i, 1, m) w[i]=e[i].b;
null=new node; null->f=null->c[0]=null->c[1]=null;
for1(i, 1, n) root[i]=new node;
for1(i, 1, m) root[i+n]=new node(i);
for1(i, 1, m) {
int u=e[i].u, v=e[i].v;
if(findrt(root[u])==findrt(root[v])) {
int pos=ask(root[u], root[v]);
if(w[pos]>e[i].b) {
cut(root[u], root[pos+n]);
cut(root[v], root[pos+n]);
link(root[u], root[i+n]);
link(root[v], root[i+n]);
}
}
else { link(root[u], root[i+n]); link(root[v], root[i+n]); }
if(findrt(root[1])==findrt(root[n])) ans=min(ans, e[i].a+w[ask(root[1], root[n])]);
}
}
int main() {
read(n); read(m);
for1(i, 1, m) read(e[i].u), read(e[i].v), read(e[i].a), read(e[i].b);
init();
printf("%d\n", ans==oo?-1:ans);
return 0;
}
为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士。魔法森林可以被看成一个包含个N节点M条边的无向图,节点标号为1..N,边标号为1..M。初始时小E同学在号节点1,隐士则住在号节点N。小E需要通过这一片魔法森林,才能够拜访到隐士。
魔法森林中居住了一些妖怪。每当有人经过一条边的时候,这条边上的妖怪就会对其发起攻击。幸运的是,在号节点住着两种守护精灵:A型守护精灵与B型守护精灵。小E可以借助它们的力量,达到自己的目的。
只要小E带上足够多的守护精灵,妖怪们就不会发起攻击了。具体来说,无向图中的每一条边Ei包含两个权值Ai与Bi。若身上携带的A型守护精灵个数不少于Ai,且B型守护精灵个数不少于Bi,这条边上的妖怪就不会对通过这条边的人发起攻击。当且仅当通过这片魔法森林的过程中没有任意一条边的妖怪向小E发起攻击,他才能成功找到隐士。
由于携带守护精灵是一件非常麻烦的事,小E想要知道,要能够成功拜访到隐士,最少需要携带守护精灵的总个数。守护精灵的总个数为A型守护精灵的个数与B型守护精灵的个数之和。
第1行包含两个整数N,M,表示无向图共有N个节点,M条边。 接下来M行,第行包含4个正整数Xi,Yi,Ai,Bi,描述第i条无向边。其中Xi与Yi为该边两个端点的标号,Ai与Bi的含义如题所述。 注意数据中可能包含重边与自环。
输出一行一个整数:如果小E可以成功拜访到隐士,输出小E最少需要携带的守护精灵的总个数;如果无论如何小E都无法拜访到隐士,输出“-1”(不含引号)。
2<=n<=50,000
0<=m<=100,000
1<=ai ,bi<=50,000
【BZOJ】3669: [Noi2014]魔法森林(lct)
原文:http://www.cnblogs.com/iwtwiioi/p/4150215.html