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 模板更新..:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N=50005, M=100005, oo=~0u>>1;
  4. int n, m;
  5. struct E {
  6. int x, y, a, b;
  7. }e[M];
  8. inline bool cmp(const E &a, const E &b) {
  9. return a.a==b.a?a.b<b.b:a.a<b.a;
  10. }
  11. struct node *null;
  12. struct node {
  13. node *c[2], *f;
  14. bool rev;
  15. int pos, w;
  16. void init() {
  17. c[0]=c[1]=f=null;
  18. rev=0;
  19. w=0;
  20. pos=0;
  21. }
  22. void upd() {
  23. if(this==null) {
  24. return;
  25. }
  26. rev=!rev;
  27. swap(c[0], c[1]);
  28. }
  29. void up() {
  30. pos=w;
  31. if(e[pos].b<e[c[0]->pos].b) {
  32. pos=c[0]->pos;
  33. }
  34. if(e[pos].b<e[c[1]->pos].b) {
  35. pos=c[1]->pos;
  36. }
  37. }
  38. void down() {
  39. if(rev) {
  40. c[0]->upd();
  41. c[1]->upd();
  42. rev=0;
  43. }
  44. }
  45. void setc(node *x, bool d) {
  46. c[d]=x;
  47. x->f=this;
  48. }
  49. bool d() {
  50. return f->c[1]==this;
  51. }
  52. bool isson() {
  53. return f->c[0]==this || f->c[1]==this;
  54. }
  55. }Po[N+M], *iT=Po, *nd[N], *ed[M];
  56. node *newnode() {
  57. iT->init();
  58. return iT++;
  59. }
  60. void rot(node *x) {
  61. node *f=x->f;
  62. bool d=x->d();
  63. if(f->isson()) {
  64. f->f->setc(x, f->d());
  65. }
  66. else {
  67. x->f=f->f;
  68. }
  69. f->setc(x->c[!d], d);
  70. x->setc(f, !d);
  71. f->up();
  72. }
  73. void splay(node *x) {
  74. static node *s[N+M], *t;
  75. int top=0;
  76. for(s[++top]=t=x; t->isson(); t=t->f) { // sb * oo
  77. s[++top]=t->f;
  78. }
  79. while(top) {
  80. s[top--]->down();
  81. }
  82. while(x->isson()) {
  83. if(!x->f->isson()) rot(x);
  84. else x->d()==x->f->d()?(rot(x->f), rot(x)):(rot(x), rot(x));
  85. }
  86. x->up();
  87. }
  88. node *access(node *x) {
  89. node *y=null;
  90. for(; x!=null; y=x, x=x->f) {
  91. splay(x);
  92. x->c[1]=y;
  93. }
  94. return y;
  95. }
  96. void mkroot(node *x) {
  97. access(x)->upd();
  98. splay(x);
  99. }
  100. void split(node *x, node *y) {
  101. mkroot(x);
  102. access(y);
  103. splay(y);
  104. }
  105. void link(node *x, node *y) {
  106. mkroot(x);
  107. x->f=y;
  108. }
  109. void cut(node *x, node *y) {
  110. split(x, y);
  111. y->c[0]=null;
  112. x->f=null;
  113. }
  114. node *findrt(node *x) {
  115. access(x);
  116. splay(x); // sb * 1
  117. for(; x->c[0]!=null; x=x->c[0]);
  118. return x;
  119. }
  120. int ask(node *x, node *y) {
  121. split(x, y);
  122. return y->pos;
  123. }
  124. int main() {
  125. scanf("%d%d", &n, &m);
  126. for(int i=1; i<=m; ++i) {
  127. scanf("%d%d%d%d", &e[i].x, &e[i].y, &e[i].a, &e[i].b);
  128. }
  129. null=iT++;
  130. null->init();
  131. for(int i=1; i<=n; ++i) {
  132. nd[i]=newnode();
  133. }
  134. e[0].b=-oo;
  135. sort(e+1, e+m+1, cmp);
  136. int ans=oo;
  137. for(int i=1; i<=m; ++i) {
  138. int x=e[i].x, y=e[i].y, b=e[i].b;
  139. ed[i]=newnode();
  140. ed[i]->pos=ed[i]->w=i;
  141. if(findrt(nd[x])==findrt(nd[y])) {
  142. int pos=ask(nd[x], nd[y]);
  143. if(e[pos].b>b) {
  144. cut(ed[pos], nd[e[pos].x]); // sb * 2
  145. cut(ed[pos], nd[e[pos].y]);
  146. link(ed[i], nd[x]);
  147. link(ed[i], nd[y]);
  148. }
  149. }
  150. else {
  151. link(ed[i], nd[x]);
  152. link(ed[i], nd[y]);
  153. }
  154. if(findrt(nd[1])==findrt(nd[n])) {
  155. ans=min(ans, e[i].a+e[ask(nd[1], nd[n])].b);
  156. }
  157. }
  158. printf("%d\n", ans==oo?-1:ans);
  159. return 0;
  160. }

  

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <string>
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <queue>
  8. #include <set>
  9. #include <map>
  10. using namespace std;
  11. typedef long long ll;
  12. #define rep(i, n) for(int i=0; i<(n); ++i)
  13. #define for1(i,a,n) for(int i=(a);i<=(n);++i)
  14. #define for2(i,a,n) for(int i=(a);i<(n);++i)
  15. #define for3(i,a,n) for(int i=(a);i>=(n);--i)
  16. #define for4(i,a,n) for(int i=(a);i>(n);--i)
  17. #define CC(i,a) memset(i,a,sizeof(i))
  18. #define read(a) a=getint()
  19. #define print(a) printf("%d", a)
  20. #define dbg(x) cout << (#x) << " = " << (x) << endl
  21. #define error(x) (!(x)?puts("error"):0)
  22. #define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
  23. 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; }
  24.  
  25. const int oo=~0u>>1, N=50005, M=100005;
  26. int w[M];
  27. struct node *null;
  28. struct node {
  29. node *f, *c[2];
  30. int pos, k, rev;
  31. node(int _p=0) { pos=k=_p; f=c[0]=c[1]=null; }
  32. void setc(node *x, bool d) { c[d]=x; x->f=this; }
  33. bool d() { return f->c[1]==this; }
  34. bool check() { return f==null || (f->c[0]!=this && f->c[1]!=this); }
  35. void upd() { if(this==null) return; rev=!rev; swap(c[0], c[1]); }
  36. 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; }
  37. void pushdown() { if(rev) rev=0, c[0]->upd(), c[1]->upd(); }
  38. };
  39. void rot(node *x) {
  40. node *f=x->f;
  41. f->pushdown(); x->pushdown(); bool d=x->d();
  42. if(f->check()) x->f=f->f;
  43. else f->f->setc(x, f->d());
  44. f->setc(x->c[!d], d);
  45. x->setc(f, !d);
  46. f->pushup();
  47. }
  48. void fix(node *x) { if(!x->check()) fix(x->f); x->pushdown(); }
  49. void splay(node *x) {
  50. fix(x);
  51. while(!x->check())
  52. if(x->f->check()) rot(x);
  53. else x->d()==x->f->d()?(rot(x->f), rot(x)):(rot(x), rot(x));
  54. x->pushup();
  55. }
  56. node *access(node *x) {
  57. node *y=null;
  58. for(; x!=null; y=x, x=x->f) splay(x), x->c[1]=y;
  59. return y;
  60. }
  61. void mkroot(node *x) { access(x)->upd(); splay(x); }
  62. void split(node *x, node *y) { mkroot(x); access(y); splay(y); }
  63. void link(node *x, node *y) { mkroot(x); x->f=y; }
  64. void cut(node *x, node *y) { split(x, y); y->c[0]->f=null; y->c[0]=null; }
  65. node *findrt(node *x) { access(x); splay(x); while(x->c[0]!=null) x=x->c[0]; return x; }
  66. int ask(node *x, node *y) { split(x, y); return y->pos; }
  67.  
  68. struct dat { int u, v, a, b; }e[M];
  69. node *root[N+M];
  70. int n, m, ans=oo;
  71.  
  72. bool cmp(const dat &a, const dat &b) { return a.a<b.a; }
  73. void init() {
  74. w[0]=-oo;
  75. sort(e+1, e+1+m, cmp);
  76. for1(i, 1, m) w[i]=e[i].b;
  77.  
  78. null=new node; null->f=null->c[0]=null->c[1]=null;
  79. for1(i, 1, n) root[i]=new node;
  80. for1(i, 1, m) root[i+n]=new node(i);
  81.  
  82. for1(i, 1, m) {
  83. int u=e[i].u, v=e[i].v;
  84. if(findrt(root[u])==findrt(root[v])) {
  85. int pos=ask(root[u], root[v]);
  86. if(w[pos]>e[i].b) {
  87. cut(root[u], root[pos+n]);
  88. cut(root[v], root[pos+n]);
  89. link(root[u], root[i+n]);
  90. link(root[v], root[i+n]);
  91. }
  92. }
  93. else { link(root[u], root[i+n]); link(root[v], root[i+n]); }
  94. if(findrt(root[1])==findrt(root[n])) ans=min(ans, e[i].a+w[ask(root[1], root[n])]);
  95. }
  96. }
  97.  
  98. int main() {
  99. read(n); read(m);
  100. for1(i, 1, m) read(e[i].u), read(e[i].v), read(e[i].a), read(e[i].b);
  101. init();
  102. printf("%d\n", ans==oo?-1:ans);
  103. return 0;
  104. }

  


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

 

【BZOJ】3669: [Noi2014]魔法森林(lct+特殊的技巧)的更多相关文章

  1. BZOJ 3669: [Noi2014]魔法森林( LCT )

    排序搞掉一维, 然后就用LCT维护加边MST. O(NlogN) ------------------------------------------------------------------- ...

  2. bzoj 3669: [Noi2014]魔法森林 (LCT)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3669 题面: 3669: [Noi2014]魔法森林 Time Limit: 30 Sec  ...

  3. bzoj 3669: [Noi2014] 魔法森林 LCT版

    Description 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看成一个包含个N节点M条边的无向图,节点标号为1..N,边标号为1..M.初始时小E同学在号节 ...

  4. BZOJ 3669: [Noi2014]魔法森林 [LCT Kruskal | SPFA]

    题目描述 为了得到书法大家的真传,小 E 同学下定决心去拜访住在魔法森林中的隐 士.魔法森林可以被看成一个包含 n 个节点 m 条边的无向图,节点标号为 1,2,3,…,n,边标号为 1,2,3,…, ...

  5. BZOJ 3669: [Noi2014]魔法森林(lct+最小生成树)

    传送门 解题思路 \(lct\)维护最小生成树.我们首先按照\(a\)排序,然后每次加入一条边,在图中维护一棵最小生成树.用并查集判断一下\(1\)与\(n\)是否联通,如果联通的话就尝试更新答案. ...

  6. bzoj 3669: [Noi2014]魔法森林

    bzoj 3669: [Noi2014]魔法森林 Description 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看成一个包含个N节点M条边的无向图,节点标号 ...

  7. bzoj 3669: [Noi2014]魔法森林 动态树

    3669: [Noi2014]魔法森林 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 363  Solved: 202[Submit][Status] ...

  8. bzoj 3669: [Noi2014]魔法森林 -- 动点spfa

    3669: [Noi2014]魔法森林 Time Limit: 30 Sec  Memory Limit: 512 MB 动点spfa Description 为了得到书法大家的真传,小E同学下定决心 ...

  9. [BZOJ 3669] [Noi2014] 魔法森林 【LCT】

    题目链接:BZOJ - 3669 题目分析 如果确定了带 x 只精灵A,那么我们就是要找一条 1 到 n 的路径,满足只经过 Ai <= x 的边,而且要使经过的边中最大的 Bi 尽量小. 其实 ...

  10. bzoj 3669: [Noi2014]魔法森林(并查集+LCT)

    Description 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看成一个包含个N节点M条边的无向图,节点标号为1..N,边标号为1..M.初始时小E同学在号节 ...

随机推荐

  1. HDU1879 kruscal 继续畅通工程

    继续畅通工程 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  2. poj 3026 bfs+prim Borg Maze

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9718   Accepted: 3263 Description The B ...

  3. apache ab压力测试报错(apr_socket_recv: Connection reset by peer (104))

    apache ab压力测试报错(apr_socket_recv: Connection reset by peer (104))   今天用apache 自带的ab工具测试,当并发量达到1000多的时 ...

  4. 2014-08-07 SSDB 使用 rocksdb 引擎

    http://www.ideawu.net/blog/archives/824.html 为了满足各位对 Facebook 出品的 rocksdb 的爱好, SSDB 数据库也可以使用 rocksdb ...

  5. 【SpringMVC】SpringMVC系列8之Servlet API 作为入参

    8.Servlet API 作为入参 8.1.概述 MVC 的 Handler 方法可以接受哪些 ServletAPI 类型的参数: HttpServletRequest HttpServletRes ...

  6. 关于Xcode6 Segue 的疑问,没有解决!

    xcode6 的segue 变化了,如图 关于前3个选项,始终没有太明白,我试验结果如下,简单地把几个viewController连接起来时,无论用show,还是showdetail,还是Presen ...

  7. 将文件放到Android模拟器的SD卡

    1.打开DDMS页面2.打开File Explorer页,如果没有,在Window –> Show View –>File Explorer3.一般就在mnt –> sdcard中4 ...

  8. 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 ...

  9. codeforces 488A. Giga Tower 解题报告

    题目链接:http://codeforces.com/problemset/problem/488/A 题目意思:给出一个数a,范围是[-10^9, 10^9],问它最少需要加的一个正整数 b 是多少 ...

  10. Material Design入门(二)

    本文主要包括以下内容 侧滑菜单DrawerLayout实现 CardView实现 DrawerLayout介绍 drawerLayout是Support Library包中实现了侧滑菜单效果的控件,可 ...