【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同学在号节 ...
随机推荐
- DrawText
该函数在指定的矩形里写入格式化的正文,根据指定的方法对正文格式化(扩展的制表符,字符对齐.折行等). int DrawText(HDC hDC, // 设备描述表句柄 LPCTSTR lpStri ...
- Linux命令之exit - 退出当前shell【返回值状态】
原文链接:http://codingstandards.iteye.com/blog/836625 (转载请注明出处) 用途说明 exit命令用于退出当前shell,在shell脚本中可以终止当前 ...
- C++复数四则运算的实现
程序主要实现复数的加减乘,数乘,取共轭功能. 将所有函数都定义为了成员函数. 使用库函数atof将字符串转换为浮点型数据. 函数主要难点在于处理输入.由于需要判断输入是选择退出还是继续,所以用字符串来 ...
- Scanner 和 String 类的常用方法
Scanner类是在jdk1.5 之后有了这个: 常用格式是: Scanner sc = new Scanner(System.in); 从以下版本开始: 1.5 构造方法摘要 Scanner(Fil ...
- iOS 利用constraint实现2个控件上下的空白是相等的
说的有点乱,先看个图把 其实这个constrant的目的就是控制两个方形的控件上方和下方的空白大小. 对于每一个方块来说,他们上方和下方的空白是相同的.这种“居中”的设计到处可见.一个控件想实现这种居 ...
- iOS 中的Push Notifications简单实现(APNS)
Android中的通知只有一种,就是Local Notifications,而iOS中除了Local Notifications外,还有一种Push Notifications.ios的这2种noti ...
- iOS 中的frame,bounds,center,transform关联
这里有一篇好文章 http://www.winddisk.com/2012/06/07/transform/ 先看几个知识点,UIView 的frame,bounds,center,transform ...
- eclipse对Java程序的移植
有些Java项目可能不在同一台计算机上开发,所以程序需要平台间进行移植,方法很简单,首先有一个最简单的项目HelloJava 当我们开发完成或者要休息了,一般都会保存然后在项目上右击,选择Close ...
- 1.python基础入门
作者:刘耀 出处:http://www.yaomr.com 欢迎转载 提示: 语法基于python3.5版本(会提示2.7版本和3.5版本的区别) Python命令行将以>>>开始, ...
- POJ 2429 GCD & LCM Inverse (Pollard rho整数分解+dfs枚举)
题意:给出a和b的gcd和lcm,让你求a和b.按升序输出a和b.若有多组满足条件的a和b,那么输出a+b最小的.思路:lcm=a*b/gcd lcm/gcd=a/gcd*b/gcd 可知a/gc ...