首页 > 其他分享 >CF1209F Koala and Notebook

CF1209F Koala and Notebook

时间:2022-10-19 20:46:07浏览次数:80  
标签:cnt Koala emplace int back tot Notebook bit CF1209F

传送门


思路

一道妙妙题。

我们考虑将一条边拆成若干个点连接的链,这条链上每条边的权值都是一位数

这样每个点一定是先尽量少经过边,这很 bfs。

对于转移,显然是选权值小的边先走。

但这可能出现一个问题,如果我要更新 \(u\),有一个 \(v_1\) 指向 \(u\) 边权为 \(x\),有一个 \(v_2\) 指向 \(u\) 边权为 \(y\),且 \(v_1\) 在队列中排在 \(v_2\) 前面,但如果 \(v_1\) 与 \(v_2\) 的答案相同, 却有 \(x > y\),这时显然不优。

因此我们将答案相同的点放在一起,每次先选最小的边进行转移,保证答案的正确性。


代码

#include<bits/stdc++.h>
#define LL long long
#define max(x...) std::max({x})
#define min(x...) std::min({x})
#define FOR(i, x, y) for(int i = (x); i <= (y); i++)
#define ROF(i, x, y) for(int i = (x); i >= (y); i--)
inline int rd()
{
    int sign = 1, re = 0; char c = getchar();
    while(!isdigit(c)){if(c == '-') sign = -1; c = getchar();}
    while(isdigit(c)){re = re * 10 + (c - '0'); c = getchar();}
    return sign * re;
}
namespace MOD
{
    const int mod = 1e9 + 7;
    inline int add(int a, int b) {return a + b >= mod ? a + b - mod : a + b;}
    inline int mul(int a, int b) {return 1ll * a * b % mod;}
    inline int sub(int a, int b) {return a - b < 0 ? a - b + mod : a - b;}
} using namespace MOD;
int n, m, ans[1000005], tot;
std::vector<int> e[1000005][10];
std::queue<std::vector<int>> q;
signed main()
{
#ifndef ONLINE_JUDGE
    freopen("test.in", "r", stdin);
    freopen("test.out", "w", stdout);
#endif
    tot = n = rd(), m = rd();
    FOR(i, 1, m)
    {
        int u = rd(), v = rd();
        int bit[10], cnt = 0, h = i;
        while(h) bit[++cnt] = h % 10, h /= 10;
        std::reverse(bit + 1, bit + 1 + cnt);
        if(cnt == 1) {e[u][bit[1]].emplace_back(v), e[v][bit[1]].emplace_back(u); continue;}
        
        e[u][bit[1]].emplace_back(tot + 1);
        FOR(j, 1, cnt - 2)
            e[tot + j][bit[j + 1]].emplace_back(tot + j + 1);
        e[tot + cnt - 1][bit[cnt]].emplace_back(v);

        e[v][bit[1]].emplace_back(tot + cnt - 1);
        FOR(j, 1, cnt - 2)
            e[tot + cnt - j][bit[j + 1]].emplace_back(tot + cnt - j - 1);
        e[tot + 1][bit[cnt]].emplace_back(u);

        tot += cnt - 1;
    }
    FOR(i, 2, tot) ans[i] = -1;
    q.push({1});
    while(!q.empty())
    {
        auto now = q.front(); q.pop();
        FOR(num, 0, 9)
        {
            std::vector<int> in;
            for(int i : now) for(int j : e[i][num])
                if(ans[j] == -1)
                {
                    ans[j] = add(mul(ans[i], 10), num);
                    in.emplace_back(j);
                }
            if(!in.empty()) q.push(in);
        }
    }
    FOR(i, 2, n) printf("%d\n", ans[i]);
    return 0;
}

标签:cnt,Koala,emplace,int,back,tot,Notebook,bit,CF1209F
From: https://www.cnblogs.com/zuytong/p/16807678.html

相关文章

  • Jupyter Notebook
    1、简介 JupyterNotebook(http://jupyter.org/)是一种Web应用,能让用户将说明文本、数学方程、代码和可视化内容全部组合到一个易于共享的文档中。简而言之,JupyterNote......
  • 在Jupyter Notebook 中输出 HTML
    在刚开始使用JupyterNotebook时,我总想使输出结果更使人满意,而不是只把结果打印出来。在我知道可以用HTML输出之前,我是这样输出一个表格的(数据来源:软科中国大学排名)。......
  • jupyter notebook导入conda环境
    创建conda环境condacreate--namefirstEnv激活环境并安装软件包condaactivatefirstEnvcondainstallscikit-learn安装和配置ipykernelcondainstall-cconda......
  • 太棒了,这才称得上 Jupyter Notebook 五大效率插件
    ​​JupyterNotebook​​​是一个很棒的教学、探索和编程环境,但其功能不足也是出了名的。幸好,有许多方法可以改进这个不错的工具,如​​JupyterNotebook​​扩展工具。......
  • Jupyter notebook 详细安装步骤
    前言:在安装​​Jupyter​​​notebook之前,确认您已安装python编译器(​​点击进入python官网​​)一、开始安装1、打开cmd命令窗口    在键盘上点击  win+r键,......
  • VScode中写Jupyter_Notebook
    在进行PIE-ENGINE编程的时候,之前总是使用cmd进行输入指令jupyternotebook打开,这其实有点繁琐,事实上,vscode里就已经内置了jupyter的插件,可以让你在vscode中写jupyter ......
  • Jupyter notebook导入Pycharm项目的.py文件里的模块及方法
    Jupyternotebook导入Pycharm项目种的.py文件里的模块及方法需要在Jupyternotebook里调用自己写的代码,过程如下。首先在Pycharm里写好一个文件,例如DCCACoef_Analysis.py......
  • Notebook交互式完成目标检测任务
    摘要:本文将介绍一种在Notebook中进行算法开发的新方式,新手也能够快速训练自己的模型。目标检测是计算机视觉中非常常用且基础的任务,但是由于目标检测任务的复杂性,往往令新......
  • Jupyter Notebook安装代码提示功能
    默认Jupyter Notebook没有安装代码提示功能,但是我们可以可通过如下命令安装和配置使得Jupyter Notebook具备代码提供功能。(确保Anaconda在环境变量里)1、电脑上搜索“Ana......
  • 安装Jupyter notebook及其启动目录
    如果没有安装Python直接安装Jupyternotebook是不可以的,前提是要安装好Python如果安装好了Python3(注意必须是Python),保证pip升级到最新版本。pip3install--upgradepip......