BZOJ-3669 魔法森林 Link-Cut-Tree
2016-03-12 20:28
316 查看
意识到背模版的重要性了,记住了原理和操作,然后手打模版残了。。颓我时间......
3669: [Noi2014]魔法森林
Time Limit: 30 Sec Memory Limit: 512 MB
Submit: 1598 Solved: 956
[Submit][Status][Discuss]
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
把变存成点,用LCT维护路径上的最大即可;读入后先按a排序,然后不断加边,如果出现环,删除环中最大边;用并查集维护连通性。
code:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; int read() { int x=0,f=1; char ch=getchar(); while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();} while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} return x*f; } #define maxm 100010*2 int father[maxm]; int n,m,ans; struct data{ int from,to,a,b; bool operator < (const data & xx) const { return a<xx.a; } }edge[maxm]; int find(int x) { if (x==father[x]) return x; father[x]=find(father[x]); return father[x]; } void merge(int x,int y) { int f1=find(x); int f2=find(y); if (f1!=f2) father[f1]=f2; } #define N 1000010*2 int f ,son [2],val ,maxx ,tmp ;bool rev ; bool isroot(int x) { return !f[x]||son[f[x]][0]!=x&&son[f[x]][1]!=x; } void rev1(int x) { if(!x) return; swap(son[x][0],son[x][1]); rev[x]^=1; } void pb(int x) { if(rev[x]) rev1(son[x][0]),rev1(son[x][1]),rev[x]=0; } void up(int x) { maxx[x]=x; if (val[maxx[son[x][0]]]>val[maxx[x]]) maxx[x]=maxx[son[x][0]]; if (val[maxx[son[x][1]]]>val[maxx[x]]) maxx[x]=maxx[son[x][1]]; } void rotate(int x) { int y=f[x],w=son[y][1]==x; son[y][w]=son[x][w^1]; if(son[x][w^1]) f[son[x][w^1]]=y; if(f[y]) { int z=f[y]; if(son[z][0]==y)son[z][0]=x; else if(son[z][1]==y)son[z][1]=x; } f[x]=f[y];f[y]=x; son[x][w^1]=y;up(y); } void splay(int x) { int s=1,i=x,y;tmp[1]=i; while(!isroot(i)) tmp[++s]=i=f[i]; while(s) pb(tmp[s--]); while(!isroot(x)) { y=f[x]; if(!isroot(y)) { if((son[f[y]][0]==y)^(son[y][0]==x)) rotate(x); else rotate(y); } rotate(x); } up(x); } void access(int x) { for(int y=0;x;y=x,x=f[x]) splay(x),son[x][1]=y,up(x); } int root(int x) { access(x);splay(x); while(son[x][0]) x=son[x][0]; return x; } void makeroot(int x) { access(x); splay(x); rev1(x); } void link(int x,int y) { makeroot(x); f[x]=y; access(x); } void cutf(int x) { access(x); splay(x); f[son[x][0]]=0; son[x][0]=0; up(x); } void cut(int x,int y) { makeroot(x); cutf(y); } int ask(int x,int y) { makeroot(x); access(y); splay(y); return maxx[y]; } int query(int x,int y) { makeroot(x); access(y);splay(y); return maxx[y]; } int main() { ans=0x7fffffff; n=read(),m=read(); for (int i=1; i<=n; i++) father[i]=i; for(int i=1; i<=m; i++) { int x=read(),y=read(),ai=read(),bi=read(); edge[i].from=x;edge[i].to=y;edge[i].a=ai;edge[i].b=bi; } sort(edge+1,edge+m+1); for (int i=1; i<=m; i++) { if (find(edge[i].from)==find(edge[i].to)) { int temp=query(edge[i].from,edge[i].to); if (val[temp]>edge[i].b) { cut(temp,edge[temp-n].from); cut(temp,edge[temp-n].to); } else { if (find(1)==find(n)) ans=min(ans,edge[i].a+val[query(1,n)]); continue; } } else merge(edge[i].from,edge[i].to); val[n+i]=edge[i].b; maxx[n+i]=n+i; link(edge[i].from,n+i); link(edge[i].to,n+i); if (find(1)==find(n)) ans=min(ans,edge[i].a+val[query(1,n)]); } if (ans==0x7fffffff) puts("-1"); else printf("%d\n",ans); return 0; }
相关文章推荐
- mac eclipse nodejs开发
- iOS面试题(二)
- textView不显示内容的问题
- BZOJ-3669 魔法森林 Link-Cut-Tree
- Java小知识
- SSH学习之一,struts框架入门
- Unicode编码的项目中CString、char* 、wstring、string的相互转换
- 第32讲项目5——输出小星星
- minicom操作
- BZOJ 3998: [TJOI2015]弦论
- opencv 图像去噪学习总结
- 摆动序列
- lsb_release: command not found 解决方法(转)
- 第一次安装和接触RubyonRails
- android中的Context(android内核学习记录)
- c++作业1-求两个数中较大者
- 第二周作业
- hibernate的各个jar包的作用
- 初学Git的一些心得体会
- 九度1088——剩下的树