uoj #3 (NOI 2014 魔法森林)
来源:互联网 发布:17173剑灵捏脸数据库 编辑:程序博客网 时间:2024/06/01 18:16
按
那么就可以求
#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <ctime>#include <string>#include <map>#include <vector>#include <stack>#include <queue>#include <utility>#include <iostream>#include <algorithm>template<class Num>void read(Num &x){ char c; int flag = 1; while((c = getchar()) < '0' || c > '9') if(c == '-') flag *= -1; x = c - '0'; while((c = getchar()) >= '0' && c <= '9') x = (x<<3) + (x<<1) + (c-'0'); x *= flag; return;}template<class Num>void write(Num x){ if(x < 0) putchar('-'), x = -x; static char s[20];int sl = 0; while(x) s[sl++] = x%10 + '0',x /= 10; if(!sl) {putchar('0');return;} while(sl) putchar(s[--sl]);}const int maxm = 1e5 + 50, maxn = 5e4 + 50;const int size = maxm + maxn, INF = 0x3f3f3f3f, Nya = -1;struct Edge{ int a, b, u, v; Edge(int a = 0,int b = 0,int u = 0,int v = 0):a(a), b(b), u(u), v(v){}}edge[maxm];#define REP(__i,__start,__end) for(int __i = (__start); __i <= (__end); __i++)bool cmp(const Edge &x,const Edge &y){ return x.a < y.a;}int n, m, _fa[maxn];int val[size], max[size];int find(int x){ return (_fa[x] == x) ? x : (_fa[x] = find(_fa[x]));}namespace LCT{#define check(x) (x) int pf[size], c[size][2], fa[size], rev[size]; void update(int x) { int maxL = max[c[x][0]], maxR = max[c[x][1]]; max[x] = x; if(val[maxL] > val[max[x]]) max[x] = maxL; if(val[maxR] > val[max[x]]) max[x] = maxR; } void pushdown(int x) { if(rev[x]) { std::swap(c[x][0], c[x][1]); rev[c[x][0]] ^= 1; rev[c[x][1]] ^= 1; rev[x] = 0; } } void rotate(int x) { int y = fa[x], z = fa[y], i, j; pushdown(y), pushdown(x); j = (c[z][1] == y), i = (c[y][1] == x); if(check(z)) c[z][j] = x; else pf[x] = pf[y], pf[y] = 0; if(c[x][i^1]) fa[c[x][i^1]] = y; fa[x] = z, fa[y] = x; c[y][i] = c[x][i^1], c[x][i^1] = y; update(y), update(x); } void Splay(int v) { int g = fa[v], h = fa[g]; pushdown(v); while(check(g)) { if(check(h)) { if((c[h][1] == g)^(c[g][1] == v)) rotate(v); else rotate(g); } rotate(v), g = fa[v], h = fa[g]; } update(v); } void access(int v) { int u = v, e = v; v = 0; while(u) { Splay(u); int &t = c[u][1]; if(check(t)) pf[t] = u, fa[t] = 0; if(check(v)) pf[v] = 0, fa[v] = u; t = v, update(u); v = u, u = pf[u]; } Splay(e); } void makeroot(int v) { access(v); rev[v] ^= 1; } void join(int v,int u) { makeroot(v); pf[v] = u; access(v); } void cut(int v,int u) { makeroot(v); access(u); c[u][0] = fa[v] = 0; } int query(int u,int v) { makeroot(u); access(v); return max[v]; }#undef check}void init(){ int a, b, u, v; read(n), read(m); REP(i, 1, m) { read(u), read(v), read(a), read(b); edge[i] = Edge(a, b, u, v); }}int solve(){ int ans = INF, calc; std::sort(edge + 1, edge + m + 1, cmp); REP(i, 1, n) _fa[i] = i; REP(i, 1, m) { val[i + n] = edge[i].b; max[i + n] = i + n; } REP(i, 1, m) { int u = edge[i].u, v = edge[i].v, t, x, y; if((x = find(u)) != (y = find(v))) { _fa[x] = y; LCT::join(u, i + n); LCT::join(v, i + n); } else if(edge[i].b < val[t = LCT::query(u, v)]) { LCT::cut(t, edge[t - n].u); LCT::cut(t, edge[t - n].v); LCT::join(u, i + n); LCT::join(v, i + n); } else continue; if(find(1) == find(n)) { calc = edge[i].a + val[LCT::query(1, n)]; ans = std::min(calc, ans); } } return (ans < INF) ? ans : Nya;}int main(){#ifndef ONLINE_JUDGE freopen("magic.in","r",stdin); freopen("magic.out","w",stdout);#endif init(), write(solve());#ifndef ONLINE_JUDGE fclose(stdin); fclose(stdout);#endif return 0;}
0 0
- uoj #3 (NOI 2014 魔法森林)
- NOI 2014魔法森林
- [NOI 2014]魔法森林
- bzoj3669 魔法森林(NOI 2014)
- NOI 2014 魔法森林 LCT
- NOI 2014 魔法森林 LCT
- NOI 2014 魔法森林 SPFA
- [BZOJ3669][UOJ#3][Noi2014][LCT]魔法森林
- [UOJ 3]【NOI2014】魔法森林:LCT
- uoj 魔法森林--LCT
- UOJ #3(【NOI2014】魔法森林-LCT区间最值)
- BZOJ 3669 NOI 2014 魔法森林 最短路/LCT
- [BZOJ 3669][NOI 2014]魔法森林(Link-Cut Tree)
- NKOJ 2991 (NOI 2014) 魔法森林 (动态树+最小生成树)
- UOJ#6 NOI-2014 随机数生成器
- 魔法森林
- [Contest] UOJ NOI Round #1
- BZOJ3669 [Noi2014]魔法森林
- java图形验证码生成工具类
- Android - Flux架构
- Intent的启动和属性(action)
- Java阻塞队列的实现
- Specialized Four-Digit NumbersPOJ
- uoj #3 (NOI 2014 魔法森林)
- CSS
- C\C++编程题之寻找丑数
- leetcode刷题一
- Ubuntu14.04简单部署LNMP环境
- 《编程之美》学而思-精确表达浮点数
- acm 网址集合
- 什么是线程安全和线程不安全
- hdu(2859)——Phalanx(dp)