Untitled
unknown
plain_text
a year ago
2.2 kB
7
Indexable
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long int
#define el <<'\n'
#define pb push_back
#define F(x) for (auto &it:x)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define cl(n, m) ((n+m-1)/m)
#define isON(n, k) (((n) >> (k)) & 1)
#define sz(x) (int)x.size()
#define dg(x) cout<<#x<<" = "<<x el;
#define Dg(x) int I=0;F(x)cout<<#x<<"["<<I++<<"] = "<<it el;
int dx[] = {0, 0, -1, 1, -1, 1, -1, 1};
int dy[] = {-1, 1, 0, 0, -1, -1, 1, 1};
const int N =1e5+5, MOD = 1000000007, oo = 0x3f3f3f3f3f3f3f3f;
//1000000007 998244353
void Wrong();
void Thaer() {
ios::sync_with_stdio(false), cout.tie(nullptr), cin.tie(nullptr);
//freopen("window.in", "r", stdin);
//freopen("pow.out", "w", stdout);
int T = 1;
// cin >> T;
while (T--)Wrong(),cout ;
}
struct Edge{
int u,v,w;
};
void Wrong() {
string s, t;
cin >> s >> t;
int n;
cin >> n;
int dis[26][26] = {};
for (int i = 0; i < 26; ++i) {
for (int j = 0; j < 26; ++j) {
if (i == j)continue;
dis[i][j] = oo;
}
}
for (int i = 0; i < n; ++i) {
char u, v;
int w;
cin >> u >> v >> w;
dis[u - 'a'][v - 'a'] = min(w, dis[u - 'a'][v - 'a']);
}
for (int i = 0; i < 26; ++i) {
for (int j = 0; j < 26; ++j) {
for (int k = 0; k < 26; ++k) {
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}
}
}
if (sz(s) != sz(t)) {
cout << -1;
return;
}
int ans = 0;
string str = "";
for (int i = 0; i < sz(s); ++i) {
int ch = 0,Dis=oo;
for (int j = 0; j < 26; ++j) {
if(dis[s[i] - 'a'][j] + dis[t[i] - 'a'][j] < Dis){
Dis = dis[s[i] - 'a'][j] + dis[t[i] - 'a'][j];
ch = j;
}
}
if (Dis==oo) {
cout << -1;
return;
}
ans += Dis;
str += char(ch + 'a');
}
cout << ans el << str;
}
int32_t main() { Thaer(); }Editor is loading...
Leave a Comment