您的位置:首页 > 理论基础 > 数据结构算法

bzoj 3669 NOI2014 魔法森林 [LCT]

2016-10-12 12:32 399 查看
3669: [Noi2014]魔法森林

Time Limit: 30 Sec Memory Limit: 512 MB

Submit: 2157 Solved: 1311

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

排序后按a大小枚举a的最终答案。

对于确定的a的大小,维护关于b的MST,LCT维护边权即可。

当1,n联通时,更新答案。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
const int INF=0x3f3f3f3f;
const int maxn = 50005;
const int maxm = 100005;
struct Node
{
int fa;
int ch[2];
bool isroot;
int rev;
int val; // val for this edge
int u,v; // if edge , the connection betwween u & v
int mx,id; // id in the LCT
}node[maxn + maxm];
int maxnode;
#define fa(x) node[x].fa
#define ch(x,d) node[x].ch[d]
#define isroot(x) node[x].isroot
#define rev(x) node[x].rev
#define val(x) node[x].val
#define u(x) node[x].u
#define v(x) node[x].v
#define mx(x) node[x].mx
#define id(x) node[x].id
inline void update(int x)
{
if(!x) return;
mx(x) = val(x); id(x) = x;
if(ch(x,0) && mx(x)<mx(ch(x,0))) mx(x)=mx(ch(x,0)),id(x)=id(ch(x,0));
if(ch(x,1) && mx(x)<mx(ch(x,1))) mx(x)=mx(ch(x,1)),id(x)=id(ch(x,1));
}
inline void rotate(int x)
{
int y=fa(x),z=fa(y);
int l = ch(y,1)==x , r = l^1;
if(isroot(y)) isroot(y) = false , isroot(x) = true;
else ch(z,ch(z,1)==y) = x;
fa(ch(x,r))=y; fa(y)=x; fa(x)=z;
ch(y,l)=ch(x,r); ch(x,r)=y;
update(y); update(x);
}
inline void pushdown(int x)
{
if(!x) return;
if(!rev(x)) return;
rev(x)^=1; swap(ch(x,0),ch(x,1));
if(ch(x,0)) rev(ch(x,0))^=1;
if(ch(x,1)) rev(ch(x,1))^=1;
}
int sta[maxn+maxm];
int top;
inline void upgrade(int x)
{
while(!isroot(x)) sta[++top]=x,x=fa(x);
sta[++top]=x;
while(top) pushdown(sta[top--]);
}
inline void splay(int x)
{
upgrade(x);
while(!isroot(x))
{
int y=fa(x),z=fa(y);
if(!isroot(y))
if(ch(y,0)==x ^ ch(z,0)==y) rotate(x);
else rotate(y);
rotate(x);
}
}
inline int access(int x)
{
int y = 0;
do
{
splay(x);
isroot(ch(x,1)) = true;
isroot(ch(x,1)=y) = false;
update(x);
x = fa(y=x);
}while(x);
return y;
}
void makeroot(int u)
{
int rt = access(u);
rev(rt) ^= 1;
}
int findroot(int x)
{
while(fa(x)) x = fa(x);
return x;
}
void cut(int u,int v)
{
makeroot(u);
access(v); splay(v);
fa(ch(v,0)) = 0;
isroot(ch(v,0)) = true;
ch(v,0) = 0;
update(v);
}
void join(int u,int v)
{
access(u); splay(u);
rev(u) ^= 1;
fa(u) = v;
}
bool query_block(int u,int v)
{
int t1 = findroot(u);
int t2 = findroot(v);
return t1==t2;
}
int query_max(int u,int v) // id of LCT
{
makeroot(u);
access(v); splay(v);
return id(v);
}
struct Road
{
int u,v;
int a,b;
bool operator < (const Road t) const
{
if(a ^ t.a) return a < t.a;
return b < t.b;
}
void read() { scanf("%d%d%d%d",&u,&v,&a,&b); }
}road[maxm];
int n,m;
void init()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++) road[i].read();
sort(road+1,road+m+1);
for(int i=1;i<=n;i++) isroot(i)=true;
maxnode = n;
}
int work()
{
int ans = INF;
for(int i=1;i<=m;i++)
{
if(query_block(road[i].u,road[i].v))
{
int id = query_max(road[i].u,road[i].v);
if(val(id) <= road[i].b) continue;
int u = u(id) , v = v(id);
cut(id,u); cut(id,v);
}
Node &tmp = node[++maxnode];
tmp.isroot = true; tmp.val = road[i].b;
tmp.u = road[i].u , tmp.v = road[i].v;
tmp.mx = tmp.val , tmp.id = maxnode;
join(maxnode,road[i].u); join(maxnode,road[i].v);
if(query_block(1,n)) smin(ans,road[i].a+val(query_max(1,n)));
}
return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("forest.in","r",stdin);
freopen("forest.out","w",stdout);
#endif
init();
int ans = work();
if(ans^INF) printf("%d",ans);
else printf("-1");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息