forked from 21171-somesh/CodeforcesSolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path33B.cpp
More file actions
104 lines (97 loc) · 2.07 KB
/
33B.cpp
File metadata and controls
104 lines (97 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/******************************************
*******************************************
*******************************************
PREPARED BY - SOMESH AWASTHI
CHANDIGARH UNIVERSITY
*******************************************
*******************************************
*******************************************/
#include<bits/stdc++.h>
using namespace std;
#define fr(i,a,b) for(long long i=a;i<b;i++)
#define io ios::sync_with_stdio(false);cin.tie(NULL);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
long int mod=1000000007;
class graph{
int size;
public:
vector<vl> dist;
graph(){
size = 26;
dist.resize(26, vl (26, INT_MAX));
fr(i, 0, 26) {
dist[i][i] = 0;
}
}
void addEdge(int u, int v, int a){
dist[u][v] = min(dist[u][v], (ll)a);
}
void floydWarshall();
};
void graph::floydWarshall() {
fr(k, 0, size) {
fr(i, 0, size) {
fr(j, 0, size) {
dist[i][j] = min(dist[i][j], (dist[i][k] + dist[k][j]));
}
}
}
}
int main()
{
io
#ifdef SOMU
clock_t startTime = clock();
freopen("input.txt","r",stdin);
#endif
string s, t;
cin >> s >> t;
graph g;
int n;
cin >> n;
while(n--) {
char x, y;
int a;
cin >> x >> y >> a;
g.addEdge(x - 'a', y - 'a', a);
}
g.floydWarshall();
if(s.size() != t.size()) {
cout << -1;
return 0;
}
int count = 0;
fr(i, 0, s.size()) {
if(s[i] != t[i]) {
ll m = INT_MAX;
ll index = 0;
fr(j, 0, 26) {
ll a = g.dist[s[i] - 'a'][j];
ll b = g.dist[t[i] - 'a'][j];
// cout << a << " " << b << " " << char(j + 'a') << '\n';
if(a >= INT_MAX || b >= INT_MAX) continue;
if(a + b < m) {
index = j;
m = a + b;
}
}
if(m >= INT_MAX) {
cout << -1;
return 0;
}
s[i] = index + 'a', t[i] = index + 'a';
// if(m == g.dist[s[i] - 'a'][t[i] - 'a']) s[i] = t[i];
count += m;
}
}
cout << count << '\n' << s;
#ifdef SOMU
cout << endl <<setprecision(20)<< double( clock() - startTime ) / (double)CLOCKS_PER_SEC<< " seconds." << endl;
#endif
return 0;
}