CodeForces 33B String Problem最短路模板题

B. String Problem
题目链接:https://codeforces.ml/problemset/problem/33/B

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Boy Valera likes strings. And even more he likes them, when they are identical. That’s why in his spare time Valera plays the following game. He takes any two strings, consisting of lower case Latin letters, and tries to make them identical. According to the game rules, with each move Valera can change one arbitrary character Ai in one of the strings into arbitrary character Bi, but he has to pay for every move a particular sum of money, equal to Wi. He is allowed to make as many moves as he needs. Since Valera is a very economical boy and never wastes his money, he asked you, an experienced programmer, to help him answer the question: what minimum amount of money should Valera have to get identical strings.

Input
The first input line contains two initial non-empty strings s and t, consisting of lower case Latin letters. The length of each string doesn’t exceed 105. The following line contains integer n (0 ≤ n ≤ 500) — amount of possible changings. Then follow n lines, each containing characters Ai and Bi (lower case Latin letters) and integer Wi (0 ≤ Wi ≤ 100), saying that it’s allowed to change character Ai into character Bi in any of the strings and spend sum of money Wi.

Output
If the answer exists, output the answer to the problem, and the resulting string. Otherwise output -1 in the only line. If the answer is not unique, output any.

Examples
inputCopy
uayd
uxxd
3
a x 8
x y 13
d c 3
outputCopy
21
uxyd
inputCopy
a
b
3
a b 2
a b 3
b a 5
outputCopy
2
b
inputCopy
abc
ab
6
a b 4
a b 7
b a 8
c b 11
c a 3
a c 0
outputCopy
-1


一道挺经典的最短路
题目大意:
给出两串字符串 s s , t t , n n 个字符转换的花费,试讲 s s , t t 字符串转换为相同字符串。
即要求出串 s s , t ,t 中不同字符间转换的最小花费,由此想到了最短路算法
跟据n个花费建图,将字母间相互转换的最小花费求出来,求和并记录即可
算法复杂度为: O ( 2 6 2 n l o g n ) O(26^2nlogn)

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> PII;

const int N = 1e6 + 1;
vector<PII> g[N];
int len[27][27];
int dist[N];
bool st[N];

void dijkstra(int x)
{
    memset(dist, 0x3f, sizeof dist);
    memset(st, 0, sizeof st);
    dist[x] = 0;
    priority_queue<PII, vector<PII>, greater<PII>> heap;
    heap.push({0, x});

    while (heap.size())
    {
        auto t = heap.top();
        heap.pop();

        int ver = t.second, distance = t.first;
        if (st[ver])
            continue;
        st[ver] = true;

        for (int i = 0; i < g[ver].size(); i++)
        {
            int j = g[ver][i].first, w = g[ver][i].second;
            if (dist[j] > distance + w)
            {
                dist[j] = distance + w;
                heap.push({dist[j], j});
            }
        }
    }
}

signed main()
{
    string s, t;
    cin >> s >> t;
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        char a, b;
        int w;
        cin >> a >> b >> w;
        g[a - 'a' + 1].push_back({b - 'a' + 1, w});
    }

    for (int i = 1; i <= 26; i++)
    {
        dijkstra(i);
        for (int j = 1; j <= 26; j++)
        {
            if (i == j)
                len[i][j] = 0;
            else
                len[i][j] = dist[j];
        }
    }
    if (s.size() != t.size())
    {
        puts("-1");
        return 0;
    }
    int cnt = 0;
    for (int i = 0; i < s.size(); i++)
    {
        if (s[i] != t[i])
        {
            int a = s[i] - 'a' + 1, b = t[i] - 'a' + 1, mini = 0x3f3f3f3f;
            for (int j = 1; j <= 26; j++)
            {
                if (len[a][j] + len[b][j] < mini)
                {
                    s[i] = 'a' + j - 1;
                    mini = len[a][j] + len[b][j];
                }
            }
            if (mini = 0x3f3f3f3f)
            {
                puts("-1");
                return 0;
            }
            else
                cnt += mini;
        }
    }
    cout << cnt << endl;
    cout << s << endl;

    return 0;
}

发布了84 篇原创文章 · 获赞 12 · 访问量 2906

猜你喜欢

转载自blog.csdn.net/qq_43294914/article/details/105480286
今日推荐