uoj #3 (NOI 2014 魔法森林)
2015-08-19 17:08
218 查看
按 aia_i 排序,然后以 bib_i 为边权维护最小生成树。
那么就可以求 ai+bia_i + b_i 得最小值啦~
那么就可以求 ai+bia_i + b_i 得最小值啦~
#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; }
相关文章推荐
- 黄聪:PHP json_encode中文乱码解决方法
- [蓝牙] 1、蓝牙核心技术了解(蓝牙协议、架构、硬件和软件笔记)
- javascript字符串
- UVA 445-Marvelous Mazes
- Linux下Redis启动/关闭/重启服务脚本
- Android开发:彻底更改工程名
- 开源数据库中间件 Vitess:一组旨在使 web 应用的 MySql 数据库的水平扩展简单化的服务器和工具集
- centos6.6 安装redis 3.0.3
- 文章标题
- elasticsearch使用
- 虚拟化--VMware vSphere HA深入解析
- DLL共享数据段
- 锁存器、触发器、寄存器和缓冲器的区别
- 会话控制学习小结
- Mybatis初学一
- java图形验证码生成工具类
- JPA基本属性注解
- 开源数据库中间件 Vitess:一组旨在使 web 应用的 MySql 数据库的水平扩展简单化的服务器和工具集
- 启动程序注入DLL
- Android之MyRefreshListView上拉加载下拉刷新