【BZOJ】3669: [Noi2014]魔法森林(lct+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=3669
首先看到题目应该可以得到我们要最小化
min{ max{a(u, v)} + max{b(u, v)} }
两个变量不好做。。。那么我们约束一个a
即按a从小到大排序,依次加边。
发现当有环出现时,去掉的是环中b最大的边。
证明:因为a是从小到大排序,因此此时答案为 a+max{b(u, v)},显然b越小越好。
然后需要link和cut操作。。。
脑洞开到这里开不动了。。。。。。。。。。。。。。。。。。。。。。。
从来没写过维护边权的lct啊啊啊啊啊啊啊啊。。。。!!!
十分犹豫后。。。看题解。。。。。。。。。。。。
QAQ
我怎么没想到将边看做一个单独的节点。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
说明我是sb。
那么本题就是将边看做一个单独的节点,然后lct即可。。
2015.6.18 UPD 模板更新..:
- #include <bits/stdc++.h>
- using namespace std;
- const int N=50005, M=100005, oo=~0u>>1;
- int n, m;
- struct E {
- int x, y, a, b;
- }e[M];
- inline bool cmp(const E &a, const E &b) {
- return a.a==b.a?a.b<b.b:a.a<b.a;
- }
- struct node *null;
- struct node {
- node *c[2], *f;
- bool rev;
- int pos, w;
- void init() {
- c[0]=c[1]=f=null;
- rev=0;
- w=0;
- pos=0;
- }
- void upd() {
- if(this==null) {
- return;
- }
- rev=!rev;
- swap(c[0], c[1]);
- }
- void up() {
- pos=w;
- if(e[pos].b<e[c[0]->pos].b) {
- pos=c[0]->pos;
- }
- if(e[pos].b<e[c[1]->pos].b) {
- pos=c[1]->pos;
- }
- }
- void down() {
- if(rev) {
- c[0]->upd();
- c[1]->upd();
- rev=0;
- }
- }
- void setc(node *x, bool d) {
- c[d]=x;
- x->f=this;
- }
- bool d() {
- return f->c[1]==this;
- }
- bool isson() {
- return f->c[0]==this || f->c[1]==this;
- }
- }Po[N+M], *iT=Po, *nd[N], *ed[M];
- node *newnode() {
- iT->init();
- return iT++;
- }
- void rot(node *x) {
- node *f=x->f;
- bool d=x->d();
- if(f->isson()) {
- f->f->setc(x, f->d());
- }
- else {
- x->f=f->f;
- }
- f->setc(x->c[!d], d);
- x->setc(f, !d);
- f->up();
- }
- void splay(node *x) {
- static node *s[N+M], *t;
- int top=0;
- for(s[++top]=t=x; t->isson(); t=t->f) { // sb * oo
- s[++top]=t->f;
- }
- while(top) {
- s[top--]->down();
- }
- while(x->isson()) {
- if(!x->f->isson()) rot(x);
- else x->d()==x->f->d()?(rot(x->f), rot(x)):(rot(x), rot(x));
- }
- x->up();
- }
- node *access(node *x) {
- node *y=null;
- for(; x!=null; y=x, x=x->f) {
- splay(x);
- x->c[1]=y;
- }
- return y;
- }
- void mkroot(node *x) {
- access(x)->upd();
- splay(x);
- }
- void split(node *x, node *y) {
- mkroot(x);
- access(y);
- splay(y);
- }
- void link(node *x, node *y) {
- mkroot(x);
- x->f=y;
- }
- void cut(node *x, node *y) {
- split(x, y);
- y->c[0]=null;
- x->f=null;
- }
- node *findrt(node *x) {
- access(x);
- splay(x); // sb * 1
- for(; x->c[0]!=null; x=x->c[0]);
- return x;
- }
- int ask(node *x, node *y) {
- split(x, y);
- return y->pos;
- }
- int main() {
- scanf("%d%d", &n, &m);
- for(int i=1; i<=m; ++i) {
- scanf("%d%d%d%d", &e[i].x, &e[i].y, &e[i].a, &e[i].b);
- }
- null=iT++;
- null->init();
- for(int i=1; i<=n; ++i) {
- nd[i]=newnode();
- }
- e[0].b=-oo;
- sort(e+1, e+m+1, cmp);
- int ans=oo;
- for(int i=1; i<=m; ++i) {
- int x=e[i].x, y=e[i].y, b=e[i].b;
- ed[i]=newnode();
- ed[i]->pos=ed[i]->w=i;
- if(findrt(nd[x])==findrt(nd[y])) {
- int pos=ask(nd[x], nd[y]);
- if(e[pos].b>b) {
- cut(ed[pos], nd[e[pos].x]); // sb * 2
- cut(ed[pos], nd[e[pos].y]);
- link(ed[i], nd[x]);
- link(ed[i], nd[y]);
- }
- }
- else {
- link(ed[i], nd[x]);
- link(ed[i], nd[y]);
- }
- if(findrt(nd[1])==findrt(nd[n])) {
- ans=min(ans, e[i].a+e[ask(nd[1], nd[n])].b);
- }
- }
- printf("%d\n", ans==oo?-1:ans);
- return 0;
- }
- #include <cstdio>
- #include <cstring>
- #include <cmath>
- #include <string>
- #include <iostream>
- #include <algorithm>
- #include <queue>
- #include <set>
- #include <map>
- using namespace std;
- typedef long long ll;
- #define rep(i, n) for(int i=0; i<(n); ++i)
- #define for1(i,a,n) for(int i=(a);i<=(n);++i)
- #define for2(i,a,n) for(int i=(a);i<(n);++i)
- #define for3(i,a,n) for(int i=(a);i>=(n);--i)
- #define for4(i,a,n) for(int i=(a);i>(n);--i)
- #define CC(i,a) memset(i,a,sizeof(i))
- #define read(a) a=getint()
- #define print(a) printf("%d", a)
- #define dbg(x) cout << (#x) << " = " << (x) << endl
- #define error(x) (!(x)?puts("error"):0)
- #define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
- inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
- const int oo=~0u>>1, N=50005, M=100005;
- int w[M];
- struct node *null;
- struct node {
- node *f, *c[2];
- int pos, k, rev;
- node(int _p=0) { pos=k=_p; f=c[0]=c[1]=null; }
- void setc(node *x, bool d) { c[d]=x; x->f=this; }
- bool d() { return f->c[1]==this; }
- bool check() { return f==null || (f->c[0]!=this && f->c[1]!=this); }
- void upd() { if(this==null) return; rev=!rev; swap(c[0], c[1]); }
- void pushup() { pos=k; if(w[c[0]->pos]>w[pos]) pos=c[0]->pos; if(w[c[1]->pos]>w[pos]) pos=c[1]->pos; }
- void pushdown() { if(rev) rev=0, c[0]->upd(), c[1]->upd(); }
- };
- void rot(node *x) {
- node *f=x->f;
- f->pushdown(); x->pushdown(); bool d=x->d();
- if(f->check()) x->f=f->f;
- else f->f->setc(x, f->d());
- f->setc(x->c[!d], d);
- x->setc(f, !d);
- f->pushup();
- }
- void fix(node *x) { if(!x->check()) fix(x->f); x->pushdown(); }
- void splay(node *x) {
- fix(x);
- while(!x->check())
- if(x->f->check()) rot(x);
- else x->d()==x->f->d()?(rot(x->f), rot(x)):(rot(x), rot(x));
- x->pushup();
- }
- node *access(node *x) {
- node *y=null;
- for(; x!=null; y=x, x=x->f) splay(x), x->c[1]=y;
- return y;
- }
- void mkroot(node *x) { access(x)->upd(); splay(x); }
- void split(node *x, node *y) { mkroot(x); access(y); splay(y); }
- void link(node *x, node *y) { mkroot(x); x->f=y; }
- void cut(node *x, node *y) { split(x, y); y->c[0]->f=null; y->c[0]=null; }
- node *findrt(node *x) { access(x); splay(x); while(x->c[0]!=null) x=x->c[0]; return x; }
- int ask(node *x, node *y) { split(x, y); return y->pos; }
- struct dat { int u, v, a, b; }e[M];
- node *root[N+M];
- int n, m, ans=oo;
- bool cmp(const dat &a, const dat &b) { return a.a<b.a; }
- void init() {
- w[0]=-oo;
- sort(e+1, e+1+m, cmp);
- for1(i, 1, m) w[i]=e[i].b;
- null=new node; null->f=null->c[0]=null->c[1]=null;
- for1(i, 1, n) root[i]=new node;
- for1(i, 1, m) root[i+n]=new node(i);
- for1(i, 1, m) {
- int u=e[i].u, v=e[i].v;
- if(findrt(root[u])==findrt(root[v])) {
- int pos=ask(root[u], root[v]);
- if(w[pos]>e[i].b) {
- cut(root[u], root[pos+n]);
- cut(root[v], root[pos+n]);
- link(root[u], root[i+n]);
- link(root[v], root[i+n]);
- }
- }
- else { link(root[u], root[i+n]); link(root[v], root[i+n]); }
- if(findrt(root[1])==findrt(root[n])) ans=min(ans, e[i].a+w[ask(root[1], root[n])]);
- }
- }
- int main() {
- read(n); read(m);
- for1(i, 1, m) read(e[i].u), read(e[i].v), read(e[i].a), read(e[i].b);
- init();
- printf("%d\n", ans==oo?-1:ans);
- return 0;
- }
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
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
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
【BZOJ】3669: [Noi2014]魔法森林(lct+特殊的技巧)的更多相关文章
- BZOJ 3669: [Noi2014]魔法森林( LCT )
排序搞掉一维, 然后就用LCT维护加边MST. O(NlogN) ------------------------------------------------------------------- ...
- bzoj 3669: [Noi2014]魔法森林 (LCT)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3669 题面: 3669: [Noi2014]魔法森林 Time Limit: 30 Sec ...
- bzoj 3669: [Noi2014] 魔法森林 LCT版
Description 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看成一个包含个N节点M条边的无向图,节点标号为1..N,边标号为1..M.初始时小E同学在号节 ...
- BZOJ 3669: [Noi2014]魔法森林 [LCT Kruskal | SPFA]
题目描述 为了得到书法大家的真传,小 E 同学下定决心去拜访住在魔法森林中的隐 士.魔法森林可以被看成一个包含 n 个节点 m 条边的无向图,节点标号为 1,2,3,…,n,边标号为 1,2,3,…, ...
- BZOJ 3669: [Noi2014]魔法森林(lct+最小生成树)
传送门 解题思路 \(lct\)维护最小生成树.我们首先按照\(a\)排序,然后每次加入一条边,在图中维护一棵最小生成树.用并查集判断一下\(1\)与\(n\)是否联通,如果联通的话就尝试更新答案. ...
- bzoj 3669: [Noi2014]魔法森林
bzoj 3669: [Noi2014]魔法森林 Description 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看成一个包含个N节点M条边的无向图,节点标号 ...
- bzoj 3669: [Noi2014]魔法森林 动态树
3669: [Noi2014]魔法森林 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 363 Solved: 202[Submit][Status] ...
- bzoj 3669: [Noi2014]魔法森林 -- 动点spfa
3669: [Noi2014]魔法森林 Time Limit: 30 Sec Memory Limit: 512 MB 动点spfa Description 为了得到书法大家的真传,小E同学下定决心 ...
- [BZOJ 3669] [Noi2014] 魔法森林 【LCT】
题目链接:BZOJ - 3669 题目分析 如果确定了带 x 只精灵A,那么我们就是要找一条 1 到 n 的路径,满足只经过 Ai <= x 的边,而且要使经过的边中最大的 Bi 尽量小. 其实 ...
- bzoj 3669: [Noi2014]魔法森林(并查集+LCT)
Description 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看成一个包含个N节点M条边的无向图,节点标号为1..N,边标号为1..M.初始时小E同学在号节 ...
随机推荐
- HDU1879 kruscal 继续畅通工程
继续畅通工程 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- poj 3026 bfs+prim Borg Maze
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9718 Accepted: 3263 Description The B ...
- apache ab压力测试报错(apr_socket_recv: Connection reset by peer (104))
apache ab压力测试报错(apr_socket_recv: Connection reset by peer (104)) 今天用apache 自带的ab工具测试,当并发量达到1000多的时 ...
- 2014-08-07 SSDB 使用 rocksdb 引擎
http://www.ideawu.net/blog/archives/824.html 为了满足各位对 Facebook 出品的 rocksdb 的爱好, SSDB 数据库也可以使用 rocksdb ...
- 【SpringMVC】SpringMVC系列8之Servlet API 作为入参
8.Servlet API 作为入参 8.1.概述 MVC 的 Handler 方法可以接受哪些 ServletAPI 类型的参数: HttpServletRequest HttpServletRes ...
- 关于Xcode6 Segue 的疑问,没有解决!
xcode6 的segue 变化了,如图 关于前3个选项,始终没有太明白,我试验结果如下,简单地把几个viewController连接起来时,无论用show,还是showdetail,还是Presen ...
- 将文件放到Android模拟器的SD卡
1.打开DDMS页面2.打开File Explorer页,如果没有,在Window –> Show View –>File Explorer3.一般就在mnt –> sdcard中4 ...
- Java for LeetCode 035 Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- codeforces 488A. Giga Tower 解题报告
题目链接:http://codeforces.com/problemset/problem/488/A 题目意思:给出一个数a,范围是[-10^9, 10^9],问它最少需要加的一个正整数 b 是多少 ...
- Material Design入门(二)
本文主要包括以下内容 侧滑菜单DrawerLayout实现 CardView实现 DrawerLayout介绍 drawerLayout是Support Library包中实现了侧滑菜单效果的控件,可 ...