CF33B TLE 求助
  • 板块学术版
  • 楼主LolaPeppa
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/3/31 21:30
  • 上次更新2024/3/31 21:38:23
查看原帖
CF33B TLE 求助
1314900
LolaPeppa楼主2024/3/31 21:30
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 30, M = 100010;
char s[M], t[M];
int n, f[N][N], output[M];
void floyd()
{
    for (int k = 0; k < 26; k ++ )
        for (int i = 0; i < 26; i ++ )
            for (int j = 0; j < 26; j ++ )
                f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
}
int main()
{
    cin >> s >> t >> n;
    if (strlen(s) != strlen(t))
    {
        puts("-1");
        return 0;
    }
    memset(f, 0x3f, sizeof f);
    for (int i = 0; i < 26; i ++ ) f[i][i] = 0;
    while (n -- )
    {
        char a, b;
        int c;
        cin >> a >> b >> c;
        f[a - 'a'][b - 'a'] = min(f[a - 'a'][b - 'a'], c);
    }
    floyd();
    int res = 0;
    for (int i = 0; i < strlen(s); i ++ )
    {
        int cost = 0x3f3f3f3f, x = 0;
        for (int j = 0; j < 26; j ++ )
        {
            if (f[s[i] - 'a'][j] + f[t[i] - 'a'][j] < cost)
            {
                cost = f[s[i] - 'a'][j] + f[t[i] - 'a'][j];
                x = j;
            }
        }
        if (cost == 0x3f3f3f3f)
        {
            puts("-1");
            return 0;
        }
        res += cost;
        output[i] = x;
    }
    printf("%d\n", res);
    for (int i = 0; i < strlen(s); i ++ ) cout << (char)(output[i] + 'a');
    return 0;
}
2024/3/31 21:30
加载中...