<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>

template<typename U, typename T>
U to( const T& src )
{
	const U result = static_cast<const U>( src );
	assert( static_cast<const T>( result ) == src );
	assert( src * 2 == src * 2 );
	return result;
}

class SquareMatrix {
public:
	SquareMatrix( int size, int defaultValue = 0 );

	SquareMatrix( const SquareMatrix& other )
        { data.insert( data.begin(), other.data.begin(), other.data.end() ); size = other.size; }

	int Size() const { return size; }

	int Get( int x, int y ) const;
	void Set( int x, int y, int value );

	void SetOnDiagonal( int value ) { for( int i = 0; i < Size(); i++ ) { Set( i, i, value ); } }

private:
	vector<int> data;
	int size;

	int getIndex( int x, int y ) const;
};

SquareMatrix::SquareMatrix( int _size, int defaultValue ) :
	size( _size )
{
	assert( size > 0 );
	data.insert( data.begin(), size * size, defaultValue );
}

int SquareMatrix::getIndex( int x, int y ) const
{
	assert( x >= 0 && x < size );
	assert( y >= 0 && y < size );

	return y * size + x;
}

int SquareMatrix::Get( int x, int y ) const
{
	assert( x >= 0 && x < size );
	assert( y >= 0 && y < size );

	const int index = getIndex( x, y );
	assert( index >= 0 && index < to<int>( data.size() ) );
	return data[index];
}

void SquareMatrix::Set( int x, int y, int value )
{
	assert( x >= 0 && x < size );
	assert( y >= 0 && y < size );

	const int index = getIndex( x, y );
	assert( index >= 0 && index < to<int>( data.size() ) );
	data[index] = value;
}

class Solution {
public:
    long long minimumCost(string source, string target,
            vector<char>& original, vector<char>& changed, vector<int>& cost);

private:
    void buildInitialDistances( SquareMatrix& distances,
            const vector<char>& original, const vector<char>& changed, const vector<int>& cost );
    void processVertex( SquareMatrix& distances, int vertex );
    void buildRealDistances( SquareMatrix& distances );
    long long findConversionCost( const string source, const string target, const SquareMatrix& distances );
};

void Solution::buildInitialDistances( SquareMatrix& distances,
    const vector<char>& original, const vector<char>& changed, const vector<int>& cost )
{
	assert( original.size() == changed.size() && original.size() == cost.size() );

	// distance to self is always zero
	distances.SetOnDiagonal( 0 );

	for( int i = 0; i < to<int>( original.size() ); i++ ) {
		const int fromIndex = original[i] - 'a';
		const int toIndex = changed[i] - 'a';
        const int currentCost = distances.Get( fromIndex, toIndex );
        if( cost[i] < currentCost ) {
		    distances.Set( fromIndex, toIndex, cost[i] );
        }
	}
}

void Solution::processVertex( SquareMatrix& distances, int vertex )
{
	SquareMatrix old( distances );
	for( int i = 0; i < distances.Size(); i++ ) {
		if( i == vertex ) {
			continue;
		}
		for( int j = 0; j < distances.Size(); j++ ) {
			if( j == vertex || j == i ) {
				continue;
			}
			const int oldDistance = old.Get( i, j );
			const int part1 = old.Get( i, vertex );
			const int part2 = old.Get( vertex, j );
			const int concatDistance = ( part1 == INT_MAX || part2 == INT_MAX ) ? INT_MAX : ( part1 + part2 );
			if( concatDistance < oldDistance ) {
				distances.Set( i, j, concatDistance );
			}
		}
	}
}

void Solution::buildRealDistances( SquareMatrix& distances )
{
	for( int i = 0; i < distances.Size(); i++ ) {
		processVertex( distances, i );
	}
}

long long Solution::findConversionCost( const string source, const string target, const SquareMatrix& distances )
{
	assert( source.length() == target.length() );

	long long result = 0;
	for( int i = 0; i < to<int>( source.size() ); i++ ) {
		const char srcChar = source[i];
		const char dstChar = target[i];
		if( srcChar == dstChar ) {
			continue;
		}
		const int srcIndex = srcChar - 'a';
		const int dstIndex = dstChar - 'a';
		const int distance = distances.Get( srcIndex, dstIndex );
		if( distance == INT_MAX ) {
			// conversion not possible
			return -1;
		}
		result += distance;
	}
	return result;
}

long long Solution::minimumCost( string source, string target, vector<char>& original, vector<char>& changed, vector<int>& cost )
{
	assert( source.length() == target.length() );
	assert( original.size() == changed.size() && original.size() == cost.size() );

	static const int AlphabetSize = 26;
	SquareMatrix distances( AlphabetSize, INT_MAX );
	buildInitialDistances( distances, original, changed, cost );
	buildRealDistances( distances );

	return findConversionCost( source, target, distances );
}
150116