【BZOJ3669】【Noi2014】魔法森林(Link-Cut Tree)
【BZOJ3669】【Noi2014】魔法森林(Link-Cut Tree)
題面
題目描述
為了得到書法大家的真傳,小 E 同學下定決心去拜訪住在魔法森林中的隱 士。魔法森林可以被看成一個包含 n 個節點 m 條邊的無向圖,節點標號為 1,2,3,…,n,邊標號為 1,2,3,…,m。初始時小 E 同學在 1 號節點,隱士則住在 n 號節點。小 E 需要通過這一片魔法森林,才能夠拜訪到隱士。
魔法森林中居住了一些妖怪。每當有人經過一條邊的時候,這條邊上的妖怪 就會對其發起攻擊。幸運的是,在 1 號節點住著兩種守護精靈:A 型守護精靈與 B 型守護精靈。小 E 可以借助它們的力量,達到自己的目的。
只要小 E 帶上足夠多的守護精靈,妖怪們就不會發起攻擊了。具體來說,無 向圖中的每一條邊 ei 包含兩個權值 ai 與 bi 。若身上攜帶的 A 型守護精靈個數不 少於 ai ,且 B 型守護精靈個數不少於 bi ,這條邊上的妖怪就不會對通過這條邊 的人發起攻擊。當且僅當通過這片魔法森林的過程中沒有任意一條邊的妖怪向 小 E 發起攻擊,他才能成功找到隱士。
由於攜帶守護精靈是一件非常麻煩的事,小 E 想要知道,要能夠成功拜訪到 隱士,最少需要攜帶守護精靈的總個數。守護精靈的總個數為 A 型守護精靈的 個數與 B 型守護精靈的個數之和。
輸入輸出格式
輸入格式:
輸入文件的第 1 行包含兩個整數 n,m,表示無向圖共有 n 個節點,m 條邊。 接下來 m 行,第i+ 1 行包含 4 個正整數 Xi,Yi,ai,bi,描述第i條無向邊。 其中Xi與 Yi為該邊兩個端點的標號,ai 與 bi 的含義如題所述。 註意數據中可能包含重邊與自環。
輸出格式:
輸出一行一個整數:如果小 E 可以成功拜訪到隱士,輸出小 E 最少需要攜 帶的守護精靈的總個數;如果無論如何小 E 都無法拜訪到隱士,輸出“-1”(不 含引號)。
輸入輸出樣例
輸入樣例#1:
4 5
1 2 19 1
2 3 8 12
2 4 12 15
1 3 17 8
3 4 1 17
輸出樣例#1:
32
輸入樣例#2:
3 1
1 2 1 1
輸出樣例#2:
-1
題解
有些東西真的就是套路
比如這道題目
先講講這道題目\(SPFA\)怎麽做
把邊按照\(a\)排序之後,依次加邊,權值為\(b\)
每次跑一邊\(SPFA\),最後求解即可
因為邊是增加的,所以每次的\(dis\)
那麽,我們接著考慮
按照\(a\)排序之後,不斷加邊,如何求最大邊權的最小值
是不是想到了貨車運輸?
很顯然,我們要求最小生成樹
但是因為邊是動態的,所以需要\(LCT\)來維護
每次新加入一條邊,如何兩個點已經聯通,
那麽檢查兩點之間的路徑的最大權值,和當前邊比較
如果更大,則斷開那條邊,把這一條邊給連接上去
否則這一條邊不用連接
但是。。。
怎麽實現?
\(LCT\)真心套路
對於\(LCT\)維護邊權
不能像樹鏈剖分那樣子,把邊權放在點權上維護
需要把邊看做一個新的節點再去進行操作(所以\(LCT\)開\(n+m\)的空間???)
到時候我一定要找時間填\(LCT\)的坑。。。。(樹鏈剖分我是真的懶得填坑了。。。)
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define MAX 51000
#define lson (t[x].ch[0])
#define rson (t[x].ch[1])
#define INF 2000000000
inline int read()
{
int x=0,t=1;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
struct Node
{
int ff,ch[2];
int rev;
int v,mi;
}t[MAX<<2];
int S[MAX],top=1;
int n,m,ans=INF;
struct Line
{
int u,v,a,b;
}e[MAX<<2];
bool operator<(Line a,Line b)
{
if(a.a!=b.a)return a.a<b.a;
return a.b<b.b;
}
bool isroot(int x){return t[t[x].ff].ch[0]!=x&&t[t[x].ff].ch[1]!=x;}
void pushdown(int x)
{
if(t[x].rev)
{
t[lson].rev^=1;
t[rson].rev^=1;
t[x].rev^=1;
swap(t[x].ch[0],t[x].ch[1]);
}
}
void pushup(int x)
{
t[x].mi=x;
if(lson&&t[t[lson].mi].v>t[t[x].mi].v)t[x].mi=t[lson].mi;
if(rson&&t[t[rson].mi].v>t[t[x].mi].v)t[x].mi=t[rson].mi;
}
void rotate(int x)
{
int y=t[x].ff,z=t[y].ff;
int k=t[y].ch[1]==x;
if(!isroot(y))t[z].ch[t[z].ch[1]==y]=x;t[x].ff=z;
t[y].ch[k]=t[x].ch[k^1];t[t[x].ch[k^1]].ff=y;
t[x].ch[k^1]=y;t[y].ff=x;
pushup(y);pushup(x);
}
void Splay(int x)
{
S[top=1]=x;
for(int i=x;!isroot(i);i=t[i].ff)S[++top]=t[i].ff;
while(top)pushdown(S[top--]);
while(!isroot(x))
{
int y=t[x].ff,z=t[y].ff;
if(!isroot(y))
(t[z].ch[0]==y)^(t[y].ch[0]==x)?rotate(x):rotate(y);
rotate(x);
}
}
void access(int x){for(int y=0;x;y=x,x=t[x].ff)Splay(x),t[x].ch[1]=y,pushup(x);}
void makeroot(int x){access(x);Splay(x);t[x].rev^=1;}
int getroot(int x){access(x);Splay(x);while(t[x].ch[0])x=t[x].ch[0];return x;}
void split(int x,int y){makeroot(x);access(y);Splay(y);}
void cut(int x,int y){split(x,y);t[y].ch[0]=t[x].ff=0;pushup(y);}
void link(int x,int y){makeroot(x);t[x].ff=y;}
int Query(int x,int y){split(x,y);return t[y].mi;}
int main()
{
n=read();m=read();
for(int i=1;i<=m;++i)
e[i].u=read(),e[i].v=read(),e[i].a=read(),e[i].b=read();
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;
if(getroot(u)==getroot(v))
{
int mm=Query(u,v);
if(t[mm].v>b)
cut(e[mm-n].u,mm),cut(e[mm-n].v,mm);
else continue;
}
t[i+n].v=b;t[i+n].mi=i+n;
link(u,i+n);link(v,i+n);
int aa=getroot(1),bb=getroot(n);
if(getroot(1)==getroot(n))
ans=min(ans,a+t[Query(1,n)].v);
}
printf("%d\n",ans==INF?-1:ans);
return 0;
}
【BZOJ3669】【Noi2014】魔法森林(Link-Cut Tree)