<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:
    import numpy as np
    def minimumCost2(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int:
        ma = {}
        let = "abcdefghijklmnopqrstuvwxyz"
        for x in let:
            ma[x]={}
            for y in let:
                ma[x][y]=-1
        for og,ch,co in zip(original,changed,cost):
            #print("*",og,ch,co)
            if ma[og][ch] == -1 or ma[og][ch] > co:
                ma[og][ch] = co

        #print("**",ma)

        for zzz in range(3):
            for i in range(1,len(let)):
                z = let[i]
                for j in range(i):
                    y = let[j]
                    for k in range(i):
                        x = let[k]
                        if not x == y:
                            #print('>>>',x,y,z,ma[x][z],ma[z][y])
                            if ma[z][x] > -1 and ma[x][y] > -1:
                                if ma[z][y] == -1:
                                    ma[z][y] = ma[z][x] + ma[x][y]
                                else:
                                    ma[z][y] = min(ma[z][y], ma[z][x] + ma[x][y])

                            #if ma[z][y] > -1 and ma[y][x] > -1:
                            #    if ma[z][x] == -1:
                            #        ma[z][x] = ma[z][y] + ma[y][x]
                            #    else:
                            #        ma[z][x] = min(ma[z][x], ma[z][y] + ma[y][x])

                            if ma[x][z] > -1 and ma[z][y] > -1:
                                if ma[x][y] == -1:
                                    ma[x][y] = ma[x][z] + ma[z][y]
                                else:
                                    ma[x][y] = min(ma[x][y], ma[x][z] + ma[z][y])

                            #if ma[y][z] > -1 and ma[z][x] > -1:
                            #    if ma[y][x] == -1:
                            #        ma[y][x] = ma[y][z] + ma[z][x]
                            #    else:
                            #        ma[y][x] = min(ma[y][x], ma[y][z] + ma[z][x])

                            if ma[x][y] > -1 and ma[y][z] > -1:
                                if ma[x][z] == -1:
                                    ma[x][z] = ma[x][y] + ma[y][z]
                                else:
                                    ma[x][z] = min(ma[x][z], ma[x][y] + ma[y][z])

                            #if ma[y][x] > -1 and ma[x][z] > -1:
                            #    if ma[y][z] == -1:
                            #        ma[y][z] = ma[y][x] + ma[x][z]
                            #    else:
                            #        ma[y][z] = min(ma[y][z], ma[y][x] + ma[x][z])


        #print("****",ma)
        
        som = 0
        for so,ta in zip(source, target):
            if not so==ta:
                #print(so,ta,ma[so][ta])
                if ma[so][ta]==-1:
                    return -1
                som += ma[so][ta]

        return som
    
    def minimumCost(self, source: str, target: str, original: list[str], changed: list[str], cost: list[int]) -> int:
        yyy = self.minimumCost2(source,target,original,changed,cost)
        return yyy
        xxx=5
        arr = [[float('inf')] * xxx for _ in range(xxx)]
        
        for i in range(xxx):
            arr[i][i] = 0
        
        for i in range(len(original)):
            val1 = ord(original[i]) - ord('a')
            val2 = ord(changed[i]) - ord('a')
            arr[val1][val2] = min(arr[val1][val2], cost[i])
        
        for i in range(xxx):
            for j in range(xxx):
                for k in range(xxx):
                    arr[j][k] = min(arr[j][k], arr[j][i] + arr[i][k])
        
        ans = 0
        print(arr)
        for i in range(len(source)):
            val1 = ord(source[i]) - ord('a')
            val2 = ord(target[i]) - ord('a')

            if val1 == val2:
                continue

            if arr[val1][val2] == float('inf'):
                return -1
            
            ans += arr[val1][val2]
        
        return ans
        
2589