CodeForces - 33B String Problem【spfa||暴力】

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
Input
uayd
uxxd
3
a x 8
x y 13
d c 3
Output
21
uxyd
Input
a
b
3
a b 2
a b 3
b a 5
Output
2
b
Input
abc
ab
6
a b 4
a b 7
b a 8
c b 11
c a 3
a c 0
Output
-1

题意:给你两个目标串,让你通过一些代价字符转换,使两个串一致。下面n行,表示Ai->Bi需要w花费;
分析:
一开始就想成单纯的字符一步转换了,其实是最短路的变形,暴力||预处理+spfa都行(不加预处理会超时);
WTM就是不暴力,心烦。。。

#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;
typedef long long LL;

const int INF = 0x3f3f3f3f; 
const int MAXN = 1000 + 10;
char a[MAXN * MAXN], b[MAXN * MAXN];
int dis[MAXN], vis[MAXN], dist[MAXN], vist[MAXN];
LL dd[MAXN][MAXN];

struct node {
    char to;
    LL cos;
}e;
vector<node> G[MAXN];

inline void spfa(char x) {
    memset(vis, 0, sizeof(vis));
    memset(dis, INF, sizeof(dis));
    queue<char> que;
    que.push(x);
    vis[x] = 1;
    dis[x] = 0;
    while(!que.empty()) {
        char u = que.front();
        que.pop();
        vis[u] = 0;
        for(int i = 0; i < G[u].size(); ++i) {
            e = G[u][i];
            if(dis[e.to] > dis[u] + e.cos) {
                dis[e.to] = dis[u] + e.cos;
                if(!vis[e.to]) {
                    vis[e.to] = 1;
                    que.push(e.to);
                }
            }
        }
    }
}

inline node spfa_t(char x) {
    memset(vist, 0, sizeof(vist));
    memset(dist, INF, sizeof(dist));
    queue<char> que;
    node ans;
    ans.cos = INF;
    que.push(x);
    vist[x] = 1;
    dist[x] = 0;
    while(!que.empty()) {
        char u = que.front();
        que.pop();
        vist[u] = 0;
        if(ans.cos > dist[u] + dis[u]) {
            ans.to = u;
            ans.cos = dist[u] + dis[u];
        }
        for(int i = 0; i < G[u].size(); ++i) {
            e = G[u][i];
            if(dist[e.to] > dist[u] + e.cos) {
                dist[e.to] = dist[u] + e.cos;
                if(!vist[e.to]) {
                    vist[e.to] = 1;
                    que.push(e.to);
                }
            }
            if(ans.cos > dist[e.to] + dis[e.to]) {
                ans.to = e.to;
                ans.cos = dist[e.to] + dis[e.to];
            }
        }
    }
    return ans;
}

inline void init() {
    for(int i = 0; i < MAXN; ++i) {
        for(int j = 0; j < MAXN; ++j) {
            dd[i][j] = INF;
        }
        dd[i][i] = 0;
    }
}

int main() {
    init(); int n; LL k;
    char ch1, ch2;
    scanf("%s\n%s\n", a, b);
    scanf("%d", &n);
    for(int i = 0; i < n; ++i) {
        getchar();
        scanf("%c %c %lld", &ch1, &ch2, &k);
        dd[ch1][ch2] = min(dd[ch1][ch2], k);
    }
    for(int i = 'a'; i <= 'z'; ++i) {
        for(int j = 'a'; j <= 'z'; ++j) {
            if(dd[i][j] != INF) {
                e.to = j;
                e.cos = dd[i][j];
                G[i].push_back(e);
            }
        }
    }
    int len1 = strlen(a);
    int len2 = strlen(b);
    if(len1 != len2) {
        printf("-1\n");
        return 0;
    }
    bool flag = false;
    LL sum = 0;
    for(int i = 0; i < len1; ++i) {
        if(a[i] == b[i]) continue;
        spfa(a[i]);
        node ans = spfa_t(b[i]);
        if(ans.cos == INF) {
            flag = true;
            break;
        }
        a[i] = ans.to; 
        sum += ans.cos;
    }
    if(flag) printf("-1\n");
    else {
        printf("%lld\n", sum);
        puts(a);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36368339/article/details/79888178
今日推荐