为了得到书法大家的真传,小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型守护精灵的个数之和。
分析:将边转点,向u v连边,然后按边的a值排序,依次将边加入维护一个动态森林,如果出现环那么把原环上的最大边删去然后更新答案,因为一开始删错了边WA了一下午。。
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<queue>
#define INF 2147483640
#define eps 1e-9
#define MAXN 200010
using namespace std;
struct Edg
{
int u,v,a,b;
friend bool operator < (Edg a,Edg b)
{
return a.a < b.a;
}
}edg[MAXN];
int n,m,ans,f[MAXN],s[MAXN],ch[MAXN][2],value[MAXN],maxn[MAXN],fa[MAXN];
bool lazy[MAXN];
inline bool isroot(int x)
{
return ch[fa[x]][0] != x && ch[fa[x]][1] != x;
}
void push_up(int x)
{
maxn[x] = x;
if(value[maxn[ch[x][0]]] > value[maxn[x]]) maxn[x] = maxn[ch[x][0]];
if(value[maxn[ch[x][1]]] > value[maxn[x]]) maxn[x] = maxn[ch[x][1]];
}
void rotate(int x)
{
int y = fa[x],z = fa[y];
int d = ch[y][0] == x ? 0 : 1;
if(!isroot(y))
{
if(ch[z][0] == y) ch[z][0] = x;
else ch[z][1] = x;
}
fa[y] = x,fa[x] = z,fa[ch[x][d^1]] = y;
ch[y][d] = ch[x][d^1],ch[x][d^1] = y;
push_up(y),push_up(x);
}
inline void push_down(int x)
{
if(!lazy[x]) return;
lazy[x]^=1,lazy[ch[x][0]]^=1;lazy[ch[x][1]]^=1;
swap(ch[x][0],ch[x][1]);
}
void splay(int x)
{
int tot = 0;s[++tot] = x;
for(int i = x;!isroot(i);i = fa[i]) s[++tot] = fa[i];
for(;tot;tot--) push_down(s[tot]);
while(!isroot(x))
{
int y = fa[x],z = fa[y];
if(!isroot(y))
{
if((ch[z][0] == y) ^ (ch[y][0] == x)) rotate(x);
else rotate(y);
}
rotate(x);
}
}
void access(int x)
{
int t = 0;
while(x)
{
splay(x);
ch[x][1] = t; //parent path 边中儿子不一定是实际儿子,但父亲一定是实际父亲
push_up(x);
t = x,x = fa[x];
}
}
void makeroot(int x) //x变成根
{
access(x),splay(x);
lazy[x]^=1;
}
void link(int x,int y)
{
makeroot(x);fa[x] = y;
}
void cut(int x,int y)
{
makeroot(x),access(y),splay(y),
ch[y][0] = fa[x] = 0;
push_up(y);
}
int find(int x)
{
if(f[x] == x) return x;
f[x] = find(f[x]);
return f[x];
}
int main()
{
ans = INF;
scanf("%d%d",&n,&m);
for(int i = 1;i <= m;i++)
scanf("%d%d%d%d",&edg[i].u,&edg[i].v,&edg[i].a,&edg[i].b);
for(int i = 1;i <= n;i++) f[i] = i;
sort(edg+1,edg+1+m);
for(int i = 1;i <= m;i++)
{
if(find(edg[i].u) != find(edg[i].v))
{
f[f[edg[i].u]] = f[edg[i].v];
value[i+n] = edg[i].b;
push_up(i+n);
link(edg[i].u,i+n);
link(edg[i].v,i+n);
}
else
{
makeroot(edg[i].u);
access(edg[i].v);
splay(edg[i].v);
if(value[maxn[edg[i].v]] > edg[i].b)
{
int t = maxn[edg[i].v];
cut(t,edg[t-n].v);
cut(t,edg[t-n].u);
value[i+n] = edg[i].b;
push_up(i+n);
link(edg[i].u,i+n);
link(edg[i].v,i+n);
}
}
if(find(1) == find(n))
{
makeroot(1);
access(n);
splay(n);
ans = min(ans,edg[i].a + value[maxn[n]]);
}
}
if(ans != INF) cout<<ans<<endl;
else cout<<-1<<endl;
}