<p>You are given two <strong>0-indexed</strong> strings <code>source</code> and <code>target</code>, both of length <code>n</code> and consisting of <strong>lowercase</strong> English letters. You are also given two <strong>0-indexed</strong> character arrays <code>original</code> and <code>changed</code>, and an integer array <code>cost</code>, where <code>cost[i]</code> represents the cost of changing the character <code>original[i]</code> to the character <code>changed[i]</code>.</p>

<p>You start with the string <code>source</code>. In one operation, you can pick a character <code>x</code> from the string and change it to the character <code>y</code> at a cost of <code>z</code> <strong>if</strong> there exists <strong>any</strong> index <code>j</code> such that <code>cost[j] == z</code>, <code>original[j] == x</code>, and <code>changed[j] == y</code>.</p>

<p>Return <em>the <strong>minimum</strong> cost to convert the string </em><code>source</code><em> to the string </em><code>target</code><em> using <strong>any</strong> number of operations. If it is impossible to convert</em> <code>source</code> <em>to</em> <code>target</code>, <em>return</em> <code>-1</code>.</p>

<p><strong>Note</strong> that there may exist indices <code>i</code>, <code>j</code> such that <code>original[j] == original[i]</code> and <code>changed[j] == changed[i]</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> source = &quot;abcd&quot;, target = &quot;acbe&quot;, original = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;c&quot;,&quot;e&quot;,&quot;d&quot;], changed = [&quot;b&quot;,&quot;c&quot;,&quot;b&quot;,&quot;e&quot;,&quot;b&quot;,&quot;e&quot;], cost = [2,5,5,1,2,20]
<strong>Output:</strong> 28
<strong>Explanation:</strong> To convert the string &quot;abcd&quot; to string &quot;acbe&quot;:
- Change value at index 1 from &#39;b&#39; to &#39;c&#39; at a cost of 5.
- Change value at index 2 from &#39;c&#39; to &#39;e&#39; at a cost of 1.
- Change value at index 2 from &#39;e&#39; to &#39;b&#39; at a cost of 2.
- Change value at index 3 from &#39;d&#39; to &#39;e&#39; at a cost of 20.
The total cost incurred is 5 + 1 + 2 + 20 = 28.
It can be shown that this is the minimum possible cost.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> source = &quot;aaaa&quot;, target = &quot;bbbb&quot;, original = [&quot;a&quot;,&quot;c&quot;], changed = [&quot;c&quot;,&quot;b&quot;], cost = [1,2]
<strong>Output:</strong> 12
<strong>Explanation:</strong> To change the character &#39;a&#39; to &#39;b&#39; change the character &#39;a&#39; to &#39;c&#39; at a cost of 1, followed by changing the character &#39;c&#39; to &#39;b&#39; at a cost of 2, for a total cost of 1 + 2 = 3. To change all occurrences of &#39;a&#39; to &#39;b&#39;, a total cost of 3 * 4 = 12 is incurred.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> source = &quot;abcd&quot;, target = &quot;abce&quot;, original = [&quot;a&quot;], changed = [&quot;e&quot;], cost = [10000]
<strong>Output:</strong> -1
<strong>Explanation:</strong> It is impossible to convert source to target because the value at index 3 cannot be changed from &#39;d&#39; to &#39;e&#39;.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= source.length == target.length &lt;= 10<sup>5</sup></code></li>
	<li><code>source</code>, <code>target</code> consist of lowercase English letters.</li>
	<li><code>1 &lt;= cost.length == original.length == changed.length &lt;= 2000</code></li>
	<li><code>original[i]</code>, <code>changed[i]</code> are lowercase English letters.</li>
	<li><code>1 &lt;= cost[i] &lt;= 10<sup>6</sup></code></li>
	<li><code>original[i] != changed[i]</code></li>
</ul>

class Solution {
private:
    // long long solve(int s, int t, int n, vector<vector<int>>& adjMat) {
    //     priority_queue<pair<int, int>, vector<pair<int, int>>,
    //                    greater<pair<int, int>>>
    //         pq;
    //     vector<long long> dis(26, INT_MAX);

    //     pq.push({0,s});
    //     dis[s] = 0;

    //     while (!pq.empty()) {
    //         // int node = pq.top().first;
    //         // long long dist = pq.top().second;
    //         int node = pq.top().second;
    //         long long dist = pq.top().first;
    //         pq.pop();

    //         for (int adjnode = 0; adjnode < 26; adjnode++) {
    //             if (adjMat[node][adjnode] != 0) {
    //                 long long adjwt = adjMat[node][adjnode];

    //                 if (dist + adjwt < dis[adjnode]) {
    //                     dis[adjnode] = dist + adjwt;
    //                     // pq.push({adjnode, dis[adjnode]});
    //                     pq.push({dis[adjnode],adjnode});

    //                 }
    //             }
    //         }
    //     }

    //     if (dis[t] == INT_MAX)
    //         return -1;

    //     return dis[t];
    // }

int D[26][26];
    void FW(vector<char>& original, vector<char>& changed, vector<int>& cost){
        fill(&D[0][0], &D[0][0]+26*26, INT_MAX);//Use C++ fill
        const int sz=original.size();
        for (int i=0; i<sz; i++){
            int row=original[i]-'a';
            int col=changed[i]-'a';
            D[row][col]=min(D[row][col], cost[i]);
        }
        for (int i=0; i<26; i++) D[i][i]=0;

        for(int k=0; k<26; k++)
            for(int i=0; i<26; i++)
                for(int j=0; j<26; j++)
                    D[i][j]=min((long long)D[i][j], (long long)D[i][k]+D[k][j]);

    }
public:
    long long minimumCost(string source, string target, vector<char>& original,
                          vector<char>& changed, vector<int>& cost) {
        int n = source.size(), m = cost.size();
        long long ans = 0;

        vector<vector<int>> adjMat(26, vector<int>(26, 0));

        for (int i = 0; i < m; i++) {
            int a = original[i] - 'a', b = changed[i] - 'a';
            // cout << a << " " << b << endl;
            adjMat[a][b] = cost[i];
        }

        FW(original, changed, cost);
        
        for (int i = 0; i < n; i++) {
            int s = source[i] - 'a', t = target[i] - 'a';
            if (s == t)
                continue;

            // long long cost = solve(s, t, m, adjMat);

            // if (cost != -1)
            //     ans += cost;
            // else
            //     return cost;

            if(D[s][t]==INT_MAX)
            return -1;

            ans+=D[s][t];
        }

        return ans;
    }
};
98194