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

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

--------------------------------------------------------------------------------------

#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
 
using namespace std;
 
const int maxn = 100009;
 
int N, M;
int par[maxn];
 
inline int getint() {
char c = getchar();
int ret = 0;
for(; !isdigit(c); c = getchar());
for(; isdigit(c); c = getchar()) ret = ret * 10 + c - '0';
return ret;
}
 
int Find(int x) {
return x == par[x] ? x : par[x] = Find(par[x]);
}
 
struct O {
int x, y, a, b;
bool operator < (const O &o) const {
return a < o.a;
}
} o[maxn];
 
struct Node *Null;
 
struct Node {
Node *ch[2], *p, *fa, *mx;
bool isRoot, rev;
int v, n;
inline void upd() {
mx = this;
if(ch[0]->mx->v > mx->v) mx = ch[0]->mx;
if(ch[1]->mx->v > mx->v) mx = ch[1]->mx;
}
inline int d() {
return p->ch[1] == this;
}
inline void setc(Node* c, int d) {
ch[d] = c;
c->p = this;
}
inline void setRoot() {
fa = p, p = Null;
isRoot = true;
}
inline void pd() {
if(rev) {
swap(ch[0], ch[1]);
ch[0]->rev ^= 1;
ch[1]->rev ^= 1;
rev = false;
}
}
} pool[maxn << 1], *stk[maxn << 1], *V[maxn], *pt;
 
void Init_LCT() {
pt = pool;
Null = pt++;
Null->fa = Null->p = Null->mx = Null;
Null->ch[0] = Null->ch[1] = Null;
Null->v = 0;
}
 
Node* newNode(int v, int n) {
pt->v = v, pt->n = n;
pt->ch[0] = pt->ch[1] = Null;
pt->p = pt->fa = Null;
pt->isRoot = true, pt->rev = false;
return pt++;
}
 
void Rot(Node* t) {
Node* p = t->p;
p->pd(), t->pd();
int d = t->d();
p->p->setc(t, p->d());
p->setc(t->ch[d ^ 1], d);
t->setc(p, d ^ 1);
if(p->isRoot) {
p->isRoot = false, t->isRoot = true;
t->fa = p->fa;
}
p->upd();
}
 
void Splay(Node* t) {
int n = 0;
for(Node* o = t; o != Null; o = o->p) stk[n++] = o;
while(n--) stk[n]->pd();
for(Node* p = t->p; p != Null; p = t->p) {
if(p->p != Null)
p->d() != t->d() ? Rot(t) : Rot(p);
Rot(t);
}
t->upd();
}
 
void Access(Node* t) {
for(Node* o = Null; t != Null; o = t, t = t->fa) {
Splay(t);
t->ch[1]->setRoot();
t->setc(o, 1);
}
}
 
void makeRoot(Node* t) {
Access(t), Splay(t);
t->rev ^= 1;
}
 
void Join(Node* x, Node* y) {
makeRoot(x), x->fa = y;
}
 
void Cut(Node* x, Node* y) {
makeRoot(x);
Access(y);
Splay(x);
x->setc(Null, 1), x->upd();
y->p = Null, y->setRoot();
}
 
Node* Path(Node* x, Node* y) {
makeRoot(x);
Access(y), Splay(y);
return y;
}
 
void Init() {
Init_LCT();
N = getint(), M = getint();
for(int i = 0; i < N; i++)
V[par[i] = i] = newNode(0, 0);
for(int i = 0; i < M; i++) {
o[i].x = getint() - 1;
o[i].y = getint() - 1;
o[i].a = getint();
o[i].b = getint();
}
}
 
void Work() {
sort(o, o + M);
int ans = 1 << 30;
for(int i = 0; i < M; i++) if(o[i].x != o[i].y) {
int u = Find(o[i].x), v = Find(o[i].y);
if(i == 8) Path(V[o[i].x], V[o[i].y]);
if(u == v && Path(V[o[i].x], V[o[i].y])->mx->v <= o[i].b)
continue;
if(u == v) {
Node* e = Path(V[o[i].x], V[o[i].y])->mx;
Cut(e, V[o[e->n].x]);
Cut(e, V[o[e->n].y]);
} else
par[u] = v;
Node* t = newNode(o[i].b, i);
Join(t, V[o[i].x]);
Join(t, V[o[i].y]);
if(Find(0) == Find(N - 1))
ans = min(ans, o[i].a + Path(V[0], V[N - 1])->mx->v);
}
printf("%d ", ans != (1 << 30) ? ans : -1);
}
 
int main() {
Init();
Work();
return 0;
}

--------------------------------------------------------------------------------------

3669: [Noi2014]魔法森林

Time Limit: 30 Sec  Memory Limit: 512 MB
Submit: 1387  Solved: 814
[Submit][Status][Discuss]

Description

为了得到书法大家的真传,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型守护精灵的个数之和。

Input

第1行包含两个整数N,M,表示无向图共有N个节点,M条边。 接下来M行,第行包含4个正整数Xi,Yi,Ai,Bi,描述第i条无向边。其中Xi与Yi为该边两个端点的标号,Ai与Bi的含义如题所述。 注意数据中可能包含重边与自环。

Output

输出一行一个整数:如果小E可以成功拜访到隐士,输出小E最少需要携带的守护精灵的总个数;如果无论如何小E都无法拜访到隐士,输出“-1”(不含引号)。

Sample Input

【输入样例1】
4 5
1 2 19 1
2 3 8 12
2 4 12 15
1 3 17 8
3 4 1 17





【输入样例2】


3 1
1 2 1 1



Sample Output

【输出样例1】

32
【样例说明1】
如果小E走路径1→2→4,需要携带19+15=34个守护精灵;
如果小E走路径1→3→4,需要携带17+17=34个守护精灵;
如果小E走路径1→2→3→4,需要携带19+17=36个守护精灵;
如果小E走路径1→3→2→4,需要携带17+15=32个守护精灵。
综上所述,小E最少需要携带32个守护精灵。



【输出样例2】


-1
【样例说明2】
小E无法从1号节点到达3号节点,故输出-1。

HINT

2<=n<=50,000


0<=m<=100,000




1<=ai ,bi<=50,000

Source

原文地址:https://www.cnblogs.com/JSZX11556/p/5153844.html