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的边加入,使得最大的b最小即可,当我们加入一条边出现环时,我们只需要删掉1到n的链上b值最大的一条边,用LCT维护即可。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<ctime>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int T;
struct splay
{
splay *ls,*rs,*fa,*maxx;
int val,id;
bool rev_mark;
splay(int _,int __);
void push_up();
void rev();
void push_down();
}*null=new splay(0,0);
splay :: splay(int _,int __)
{
ls=rs=fa=null;
maxx=this;
val=_;
id=__;
rev_mark=0;
}
void splay :: rev()
{
rev_mark^=1;
swap(ls,rs);
}
void splay :: push_down()
{
if(rev_mark)
{
if(ls!=null) ls->rev();
if(rs!=null) rs->rev();
rev_mark=0;
}
}
void splay :: push_up()
{
maxx=this;
if((ls->maxx->val)>(maxx->val)) maxx=ls->maxx;
if((rs->maxx->val)>(maxx->val)) maxx=rs->maxx;
}
void right(splay *x)
{
splay *y=x->fa;
y->ls=x->rs;
x->rs->fa=y;
x->fa=y->fa;
x->rs=y;
if(y==y->fa->ls) y->fa->ls=x;
else if(y==y->fa->rs) y->fa->rs=x;
y->fa=x;
y->push_up();
}
void left(splay *x)
{
splay *y=x->fa;
y->rs=x->ls;
x->ls->fa=y;
x->fa=y->fa;
x->ls=y;
if(y==y->fa->ls) y->fa->ls=x;
else if(y==y->fa->rs) y->fa->rs=x;
y->fa=x;
y->push_up();
}
void push_down(splay *x)
{
if(x->fa!=null && (x==x->fa->ls || x==x->fa->rs)) push_down(x->fa);
x->push_down();
}
void splaying(splay *x)
{
push_down(x);
while(1)
{
splay *y=x->fa;
splay *z=y->fa;
if(x!=y->ls && x!=y->rs) break;
if(y!=z->ls && y!=z->rs)
{
if(x==y->ls) right(x);
else if(x==y->rs) left(x);
break;
}
if(x==y->ls)
{
if(y==z->ls) right(y);
right(x);
}
else if(x==y->rs)
{
if(y==z->rs) left(y);
left(x);
}
}
x->push_up();
}
void access(splay *x)
{
splay *y=null;
while(x!=null)
{
splaying(x);
x->rs=y;
x->push_up();
y=x;
x=x->fa;
}
}
void move_to_root(splay *x)
{
access(x);
splaying(x);
x->rev();
}
void link(splay *x,splay *y)
{
move_to_root(x);
x->fa=y;
}
void cut(splay *x,splay *y)
{
move_to_root(x);
access(y);
splaying(y);
y->ls=null;
y->push_up();
x->fa=null;
}
splay* b[100010];
splay* p[100010];
bool pl(splay *x,splay *y)
{
move_to_root(x);
access(y);
splaying(y);
return (x==x->fa->ls || x==x->fa->rs);
}
splay* get_maxx(splay *x,splay *y)
{
move_to_root(x);
access(y);
splaying(y);
return y->maxx;
}
struct bian
{
int l,r,a,b;
bool operator < (bian x) const
{
return a<x.a;
}
}a[100010];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
scanf("%d%d%d%d",&a[i].l,&a[i].r,&a[i].a,&a[i].b);
sort(a+1,a+1+m);
for(int i=1;i<=m;i++) b[i]=new splay(a[i].b,i);
for(int i=1;i<=n;i++) p[i]=new splay(0,0);
int ans=2147483647;
for(int i=1;i<=m;i++)
{
if(a[i].l==a[i].r) continue;
if(pl(p[a[i].l],p[a[i].r]))
{
splay *mid=get_maxx(p[a[i].l],p[a[i].r]);
if(mid->val>a[i].b)
{
int id=mid->id;
cut(p[a[id].l],mid);
cut(mid,p[a[id].r]);
link(p[a[i].l],b[i]);
link(b[i],p[a[i].r]);
}
}
else
{
link(p[a[i].l],b[i]);
link(b[i],p[a[i].r]);
}
if(pl(p[1],p[n]))
ans=min(ans,a[i].a+get_maxx(p[1],p[n])->val);
}
if(ans==2147483647) cout<<-1;
else cout<<ans;
return 0;
}