uoj #3 (NOI 2014 魔法森林)

3 篇文章 0 订阅


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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 JavaScript 编写的记忆游戏(附源代码)   项目:JavaScript 记忆游戏(附源代码) 记忆检查游戏是一个使用 HTML5、CSS 和 JavaScript 开发的简单项目。这个游戏是关于测试你的短期 记忆技能。玩这个游戏 时,一系列图像会出现在一个盒子形状的区域中 。玩家必须找到两个相同的图像并单击它们以使它们消失。 如何运行游戏? 记忆游戏项目仅包含 HTML、CSS 和 JavaScript。谈到此游戏的功能,用户必须单击两个相同的图像才能使它们消失。 点击卡片或按下键盘键,通过 2 乘 2 旋转来重建鸟儿对,并发现隐藏在下面的图像! 如果翻开的牌面相同(一对),您就赢了,并且该对牌将从游戏中消失! 否则,卡片会自动翻面朝下,您需要重新尝试! 该游戏包含大量的 javascript 以确保游戏正常运行。 如何运行该项目? 要运行此游戏,您不需要任何类型的本地服务器,但需要浏览器。我们建议您使用现代浏览器,如 Google Chrome 和 Mozilla Firefox, 以获得更好、更优化的游戏体验。要玩游戏,首先,通过单击 memorygame-index.html 文件在浏览器中打开游戏。 演示: 该项目为国外大神项目,可以作为毕业设计的项目,也可以作为大作业项目,不用担心代码重复,设计重复等,如果需要对项目进行修改,需要具备一定基础知识。 注意:如果装有360等杀毒软件,可能会出现误报的情况,源码本身并无病毒,使用源码时可以关闭360,或者添加信任。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值