您的位置:首页 > 其它

【BZOJ】3669: [Noi2014]魔法森林(lct+特殊的技巧)

2014-12-08 00:03 393 查看
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即可。。

2015.6.18 UPD 模板更新..:

#include <bits/stdc++.h>
using namespace std;
const int N=50005, M=100005, oo=~0u>>1;
int n, m;
struct E {
int x, y, a, b;
}e[M];
inline bool cmp(const E &a, const E &b) {
return a.a==b.a?a.b<b.b:a.a<b.a;
}
struct node *null;
struct node {
node *c[2], *f;
bool rev;
int pos, w;
void init() {
c[0]=c[1]=f=null;
rev=0;
w=0;
pos=0;
}
void upd() {
if(this==null) {
return;
}
rev=!rev;
swap(c[0], c[1]);
}
void up() {
pos=w;
if(e[pos].b<e[c[0]->pos].b) {
pos=c[0]->pos;
}
if(e[pos].b<e[c[1]->pos].b) {
pos=c[1]->pos;
}
}
void down() {
if(rev) {
c[0]->upd();
c[1]->upd();
rev=0;
}
}
void setc(node *x, bool d) {
c[d]=x;
x->f=this;
}
bool d() {
return f->c[1]==this;
}
bool isson() {
return f->c[0]==this || f->c[1]==this;
}
}Po[N+M], *iT=Po, *nd
, *ed[M];
node *newnode() {
iT->init();
return iT++;
}
void rot(node *x) {
node *f=x->f;
bool d=x->d();
if(f->isson()) {
f->f->setc(x, f->d());
}
else {
x->f=f->f;
}
f->setc(x->c[!d], d);
x->setc(f, !d);
f->up();
}
void splay(node *x) {
static node *s[N+M], *t;
int top=0;
for(s[++top]=t=x; t->isson(); t=t->f) { // sb * oo
s[++top]=t->f;
}
while(top) {
s[top--]->down();
}
while(x->isson()) {
if(!x->f->isson()) rot(x);
else x->d()==x->f->d()?(rot(x->f), rot(x)):(rot(x), rot(x));
}
x->up();
}
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]=null;
x->f=null;
}
node *findrt(node *x) {
access(x);
splay(x); // sb * 1
for(; x->c[0]!=null; x=x->c[0]);
return x;
}
int ask(node *x, node *y) {
split(x, y);
return y->pos;
}
int main() {
scanf("%d%d", &n, &m);
for(int i=1; i<=m; ++i) {
scanf("%d%d%d%d", &e[i].x, &e[i].y, &e[i].a, &e[i].b);
}
null=iT++;
null->init();
for(int i=1; i<=n; ++i) {
nd[i]=newnode();
}
e[0].b=-oo;
sort(e+1, e+m+1, cmp);
int ans=oo;
for(int i=1; i<=m; ++i) {
int x=e[i].x, y=e[i].y, b=e[i].b;
ed[i]=newnode();
ed[i]->pos=ed[i]->w=i;
if(findrt(nd[x])==findrt(nd[y])) {
int pos=ask(nd[x], nd[y]);
if(e[pos].b>b) {
cut(ed[pos], nd[e[pos].x]); // sb * 2
cut(ed[pos], nd[e[pos].y]);
link(ed[i], nd[x]);
link(ed[i], nd[y]);
}
}
else {
link(ed[i], nd[x]);
link(ed[i], nd[y]);
}
if(findrt(nd[1])==findrt(nd
)) {
ans=min(ans, e[i].a+e[ask(nd[1], nd
)].b);
}
}
printf("%d\n", ans==oo?-1:ans);
return 0;
}


  

#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
)) ans=min(ans, e[i].a+w[ask(root[1], root
)]);
}
}

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;
}


  

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: