[CF1202B] You Are Given a Decimal String(最短路)

Posted qrsikno

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[CF1202B] You Are Given a Decimal String(最短路)相关的知识,希望对你有一定的参考价值。

Description

今天突然想来发一篇博客防死

[Portal][https://vjudge.net/problem/2650668/origin]

定义被x-y生成器生成的序列为, 一开始有一个数字S = 0, 每次输出S % 10, 然后把这个数字加上x或y.

现在给你一个串, 对于$0\leq x, y \leq 9 $要你计算至少要在串中插入几个数位才能将其变成正确的x - y生成器生成的串

字符串长度\(\leq 2e6\)

Solution

这种数字之间跳来跳去的直接考虑最短路.

\(i\)\((i + x) \mod 10\), 连一条边权为1的单向边.

\(i\)\((i + y) \mod 10\), 连一条边权为1的单向边.

\(dis[i][i] = inf\)

直接跑floyd即可, 那么答案就是数字之间的距离-1的和.

Code

#include<bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = (a), i##_end_ = (b); i <= i##_end_; ++i)
#define drep(i, a, b) for(int i = (a), i##_end_ = (b); i >= i##_end_; --i)
#define clar(a, b) memset((a), (b), sizeof(a))
#define debug(...) fprintf(stderr, __VA_ARGS__)
typedef long long LL;
typedef long double LD;
const double pi = acos(-1);
const int BUF_SIZE = (int)1e6 + 10;
template <typename T> inline bool chkmax(T &a, const T &b) return a < b ? a = b, 1 : 0;
template <typename T> inline bool chkmin(T &a, const T &b) return a > b ? a = b, 1 : 0;
LL read() 
    char ch = getchar();
    LL x = 0, flag = 1;
    for (;!isdigit(ch); ch = getchar()) if (ch == '-') flag *= -1;
    for (;isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
    return x * flag;


const int Maxn = 2e6 + 9;
char s[Maxn];

void Init() 
    scanf("%s", s + 1);


int dis[10][10];
int calc(int x, int y) 
    clar(dis, 0x3f);
    rep (i, 0, 9) dis[i][(i + x) % 10] = 1, dis[i][(i + y) % 10] = 1;

    rep (k, 0, 9)
        rep (i, 0, 9)
            rep (j, 0, 9) dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);

    int ans = 0;
    rep (i, 1, strlen(s + 1) - 1) 
        if (dis[s[i] - '0'][s[i + 1] - '0'] == 0x3f3f3f3f) return -1;
        ans += max(dis[s[i] - '0'][s[i + 1] - '0'] - 1, 0);
    
    return ans;


void Solve() 
    rep (i, 0, 9) 
        rep (j, 0, 9) printf("%d ", calc(i, j));
        puts("");
    


int main() 
//  freopen("bosky.in", "r", stdin);
//  freopen("bosky.out", "w", stdout);

    Init();
    Solve();

#ifdef Qrsikno
    debug("\nRunning time: %.3lf(s)\n", clock() * 1.0 / CLOCKS_PER_SEC);
#endif
    return 0;

以上是关于[CF1202B] You Are Given a Decimal String(最短路)的主要内容,如果未能解决你的问题,请参考以下文章

codeforces1202B You Are Given a Decimal String... 记忆化搜索或Floyd最短路算法

[CF1039D]You Are Given a Tree

[CF1039D]You Are Given a Tree

「CF1039D」You Are Given a Tree

CF1039D You Are Given a Tree [根号分治]

[CF1039D]You Are Given a Tree[贪心+根号分治]