uoj #3 (NOI 2014 魔法森林)

来源:互联网 发布:17173剑灵捏脸数据库 编辑:程序博客网 时间:2024/06/01 18:16


ai 排序,然后以 bi 为边权维护最小生成树。

那么就可以求 ai+bi 得最小值啦~



#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
原创粉丝点击