| 模型 | 模型答案 | 正确答案 |
|---|---|---|
| 金声玉亮2.0 | ```python if u not in dist: dist[u] = {} if v not in dist: dist[v] = {} dist[u][v] = min(dist[u].get(v, INF), cost[i]) total_cost = 0 for i in range(len(source)): u, v = ord(source[i])-ord('a'), ord(target[i])-ord('a') if u != v: min_cost = memoization(u, v) if min_cost == INF: return -1 total_cost += min_cost return total_cost ``` |
class Solution(object): def minimumCost(self, source, target, original, changed, cost): """ :type source: str :type target: str :type original: List[str] :type changed: List[str] :type cost: List[int] :rtype: int """ INF = float("inf") def dijkstra(start): best = {start:0} min_heap = [(0, start)] while min_heap: curr, u = heapq.heappop(min_heap) if curr > best[u]: continue if u not in dist: continue for v, w in dist[u].items(): if v in best and best[v] <= curr+w: continue best[v] = curr+w heapq.heappush(min_heap, (best[v], v)) return best memo = {} def memoization(u, v): if u not in memo: memo[u] = dijkstra(u) return memo[u][v] if v in memo[u] else INF dist = {} for i in range(len(original)): u, v = ord(original[i])-ord('a'), ord(changed[i])-ord('a') if u not in dist: dist[u] = {v:INF} if v not in dist[u]: dist[u][v] = INF dist[u][v] = min(dist[u][v], cost[i]) result = sum(memoization(ord(source[i])-ord('a'), ord(target[i])-ord('a')) for i in range(len(source))) return result if result != INF else -1 |
| deepseek-r1 | ```python current_cost = cost[i] if u not in dist: dist[u] = {} if v not in dist[u] or current_cost < dist[u][v]: dist[u][v] = current_cost total = 0 for s, t in zip(source, target): if s == t: continue u = ord(s) - ord('a') v = ord(t) - ord('a') min_cost = memoization(u, v) if min_cost == INF: return -1 total += min_cost return total ``` |
class Solution(object): def minimumCost(self, source, target, original, changed, cost): """ :type source: str :type target: str :type original: List[str] :type changed: List[str] :type cost: List[int] :rtype: int """ INF = float("inf") def dijkstra(start): best = {start:0} min_heap = [(0, start)] while min_heap: curr, u = heapq.heappop(min_heap) if curr > best[u]: continue if u not in dist: continue for v, w in dist[u].items(): if v in best and best[v] <= curr+w: continue best[v] = curr+w heapq.heappush(min_heap, (best[v], v)) return best memo = {} def memoization(u, v): if u not in memo: memo[u] = dijkstra(u) return memo[u][v] if v in memo[u] else INF dist = {} for i in range(len(original)): u, v = ord(original[i])-ord('a'), ord(changed[i])-ord('a') if u not in dist: dist[u] = {v:INF} if v not in dist[u]: dist[u][v] = INF dist[u][v] = min(dist[u][v], cost[i]) result = sum(memoization(ord(source[i])-ord('a'), ord(target[i])-ord('a')) for i in range(len(source))) return result if result != INF else -1 |
| gpto1 | ```python if u not in dist: dist[u] = {} dist[u][v] = min(dist[u].get(v, INF), cost[i]) total_cost = 0 for i in range(len(source)): u = ord(source[i]) - ord('a') v = ord(target[i]) - ord('a') if u == v: continue c = memoization(u, v) if c == INF: return -1 total_cost += c return total_cost ``` |
class Solution(object): def minimumCost(self, source, target, original, changed, cost): """ :type source: str :type target: str :type original: List[str] :type changed: List[str] :type cost: List[int] :rtype: int """ INF = float("inf") def dijkstra(start): best = {start:0} min_heap = [(0, start)] while min_heap: curr, u = heapq.heappop(min_heap) if curr > best[u]: continue if u not in dist: continue for v, w in dist[u].items(): if v in best and best[v] <= curr+w: continue best[v] = curr+w heapq.heappush(min_heap, (best[v], v)) return best memo = {} def memoization(u, v): if u not in memo: memo[u] = dijkstra(u) return memo[u][v] if v in memo[u] else INF dist = {} for i in range(len(original)): u, v = ord(original[i])-ord('a'), ord(changed[i])-ord('a') if u not in dist: dist[u] = {v:INF} if v not in dist[u]: dist[u][v] = INF dist[u][v] = min(dist[u][v], cost[i]) result = sum(memoization(ord(source[i])-ord('a'), ord(target[i])-ord('a')) for i in range(len(source))) return result if result != INF else -1 |
| gpt-4o-2024-08-06 | ```python if u not in dist: dist[u] = {} if v not in dist: dist[v] = {} if v not in dist[u] or dist[u][v] > cost[i]: dist[u][v] = cost[i] total_cost = 0 for i in range(len(source)): if source[i] == target[i]: continue u, v = ord(source[i]) - ord('a'), ord(target[i]) - ord('a') min_cost = memoization(u, v) if min_cost == INF: return -1 total_cost += min_cost return total_cost ``` |
class Solution(object): def minimumCost(self, source, target, original, changed, cost): """ :type source: str :type target: str :type original: List[str] :type changed: List[str] :type cost: List[int] :rtype: int """ INF = float("inf") def dijkstra(start): best = {start:0} min_heap = [(0, start)] while min_heap: curr, u = heapq.heappop(min_heap) if curr > best[u]: continue if u not in dist: continue for v, w in dist[u].items(): if v in best and best[v] <= curr+w: continue best[v] = curr+w heapq.heappush(min_heap, (best[v], v)) return best memo = {} def memoization(u, v): if u not in memo: memo[u] = dijkstra(u) return memo[u][v] if v in memo[u] else INF dist = {} for i in range(len(original)): u, v = ord(original[i])-ord('a'), ord(changed[i])-ord('a') if u not in dist: dist[u] = {v:INF} if v not in dist[u]: dist[u][v] = INF dist[u][v] = min(dist[u][v], cost[i]) result = sum(memoization(ord(source[i])-ord('a'), ord(target[i])-ord('a')) for i in range(len(source))) return result if result != INF else -1 |
| Meta-Llama-3.1-405B-Instruct-Turbo | ```python if u not in dist: dist[u] = {} if v not in dist[u]: dist[u][v] = cost[i] else: dist[u][v] = min(dist[u][v], cost[i]) total_cost = 0 for i in range(len(source)): u, v = ord(source[i])-ord('a'), ord(target[i])-ord('a') if u == v: continue cost = memoization(u, v) if cost == INF: return -1 total_cost += cost return total_cost ``` |
class Solution(object): def minimumCost(self, source, target, original, changed, cost): """ :type source: str :type target: str :type original: List[str] :type changed: List[str] :type cost: List[int] :rtype: int """ INF = float("inf") def dijkstra(start): best = {start:0} min_heap = [(0, start)] while min_heap: curr, u = heapq.heappop(min_heap) if curr > best[u]: continue if u not in dist: continue for v, w in dist[u].items(): if v in best and best[v] <= curr+w: continue best[v] = curr+w heapq.heappush(min_heap, (best[v], v)) return best memo = {} def memoization(u, v): if u not in memo: memo[u] = dijkstra(u) return memo[u][v] if v in memo[u] else INF dist = {} for i in range(len(original)): u, v = ord(original[i])-ord('a'), ord(changed[i])-ord('a') if u not in dist: dist[u] = {v:INF} if v not in dist[u]: dist[u][v] = INF dist[u][v] = min(dist[u][v], cost[i]) result = sum(memoization(ord(source[i])-ord('a'), ord(target[i])-ord('a')) for i in range(len(source))) return result if result != INF else -1 |
| Meta-Llama-3.1-70B-Instruct-Turbo | ```python if u not in dist: dist[u] = {} dist[u][v] = min(dist[u].get(v, INF), cost[i]) for i in range(26): if i not in dist: continue for j in dist[i]: for k in dist: if j in dist[k]: dist[i][j] = min(dist[i][j], memoization(i, k) + dist[k][j]) res = 0 for i in range(n): if source[i] == target[i]: continue res += memoization(ord(source[i])-ord('a'), ord(target[i])-ord('a')) if res >= INF: return -1 return res ``` |
class Solution(object): def minimumCost(self, source, target, original, changed, cost): """ :type source: str :type target: str :type original: List[str] :type changed: List[str] :type cost: List[int] :rtype: int """ INF = float("inf") def dijkstra(start): best = {start:0} min_heap = [(0, start)] while min_heap: curr, u = heapq.heappop(min_heap) if curr > best[u]: continue if u not in dist: continue for v, w in dist[u].items(): if v in best and best[v] <= curr+w: continue best[v] = curr+w heapq.heappush(min_heap, (best[v], v)) return best memo = {} def memoization(u, v): if u not in memo: memo[u] = dijkstra(u) return memo[u][v] if v in memo[u] else INF dist = {} for i in range(len(original)): u, v = ord(original[i])-ord('a'), ord(changed[i])-ord('a') if u not in dist: dist[u] = {v:INF} if v not in dist[u]: dist[u][v] = INF dist[u][v] = min(dist[u][v], cost[i]) result = sum(memoization(ord(source[i])-ord('a'), ord(target[i])-ord('a')) for i in range(len(source))) return result if result != INF else -1 |
| DeepSeek-Coder-V2-Lite-Instruct | ```python if u not in dist: dist[u] = {} if v not in dist[u] or dist[u][v] > cost[i]: dist[u][v] = cost[i] total_cost = 0 for i in range(len(source)): if source[i] == target[i]: continue u, v = ord(source[i])-ord('a'), ord(target[i])-ord('a') cost_to_convert = memoization(u, v) if cost_to_convert == INF: return -1 total_cost += cost_to_convert return total_cost ``` |
class Solution(object): def minimumCost(self, source, target, original, changed, cost): """ :type source: str :type target: str :type original: List[str] :type changed: List[str] :type cost: List[int] :rtype: int """ INF = float("inf") def dijkstra(start): best = {start:0} min_heap = [(0, start)] while min_heap: curr, u = heapq.heappop(min_heap) if curr > best[u]: continue if u not in dist: continue for v, w in dist[u].items(): if v in best and best[v] <= curr+w: continue best[v] = curr+w heapq.heappush(min_heap, (best[v], v)) return best memo = {} def memoization(u, v): if u not in memo: memo[u] = dijkstra(u) return memo[u][v] if v in memo[u] else INF dist = {} for i in range(len(original)): u, v = ord(original[i])-ord('a'), ord(changed[i])-ord('a') if u not in dist: dist[u] = {v:INF} if v not in dist[u]: dist[u][v] = INF dist[u][v] = min(dist[u][v], cost[i]) result = sum(memoization(ord(source[i])-ord('a'), ord(target[i])-ord('a')) for i in range(len(source))) return result if result != INF else -1 |
| Qwen2-72B-Instruct | ```python if u not in dist: dist[u] = {} if v not in dist: dist[v] = {} if v not in dist[u] or dist[u][v] > cost[i]: dist[u][v] = cost[i] res = 0 for i in range(len(source)): u, v = ord(source[i])-ord('a'), ord(target[i])-ord('a') if u == v: continue if u not in dist or v not in dist[u]: return -1 res += memoization(u, v) return res ``` |
class Solution(object): def minimumCost(self, source, target, original, changed, cost): """ :type source: str :type target: str :type original: List[str] :type changed: List[str] :type cost: List[int] :rtype: int """ INF = float("inf") def dijkstra(start): best = {start:0} min_heap = [(0, start)] while min_heap: curr, u = heapq.heappop(min_heap) if curr > best[u]: continue if u not in dist: continue for v, w in dist[u].items(): if v in best and best[v] <= curr+w: continue best[v] = curr+w heapq.heappush(min_heap, (best[v], v)) return best memo = {} def memoization(u, v): if u not in memo: memo[u] = dijkstra(u) return memo[u][v] if v in memo[u] else INF dist = {} for i in range(len(original)): u, v = ord(original[i])-ord('a'), ord(changed[i])-ord('a') if u not in dist: dist[u] = {v:INF} if v not in dist[u]: dist[u][v] = INF dist[u][v] = min(dist[u][v], cost[i]) result = sum(memoization(ord(source[i])-ord('a'), ord(target[i])-ord('a')) for i in range(len(source))) return result if result != INF else -1 |
| baichuan4 | ```python if u not in dist: dist[u] = {} dist[u][v] = cost[i] # Initialize the distances from each character in source to each character in target dp = [[INF] * len(target) for _ in range(len(source))] Base case Base case: if source and target are the same at a position, the cost is 0 for i in range(len(source)): if source[i] == target[i]: dp[i][i] = 0 # Populate the dp table using the memoized Dijkstra's algorithm for i in range(len(source)): for j in range(len(target)): if i != j: dp[i][j] = memoization(ord(source[i]) - ord('a'), ord(target[j]) - ord('a')) # Use dynamic programming to find the minimum to convert source to convert source to target for length in range(2, len(source) + 1): for i in range(len(source) - length + 1): j = i + length - 1 for k in range(i, j): dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j]) # If the last entry in the dp table is INF, it means conversion is impossible return -1 if dp[0][-1] == INF else dp[0][-1] ``` |
class Solution(object): def minimumCost(self, source, target, original, changed, cost): """ :type source: str :type target: str :type original: List[str] :type changed: List[str] :type cost: List[int] :rtype: int """ INF = float("inf") def dijkstra(start): best = {start:0} min_heap = [(0, start)] while min_heap: curr, u = heapq.heappop(min_heap) if curr > best[u]: continue if u not in dist: continue for v, w in dist[u].items(): if v in best and best[v] <= curr+w: continue best[v] = curr+w heapq.heappush(min_heap, (best[v], v)) return best memo = {} def memoization(u, v): if u not in memo: memo[u] = dijkstra(u) return memo[u][v] if v in memo[u] else INF dist = {} for i in range(len(original)): u, v = ord(original[i])-ord('a'), ord(changed[i])-ord('a') if u not in dist: dist[u] = {v:INF} if v not in dist[u]: dist[u][v] = INF dist[u][v] = min(dist[u][v], cost[i]) result = sum(memoization(ord(source[i])-ord('a'), ord(target[i])-ord('a')) for i in range(len(source))) return result if result != INF else -1 |
| doubao | ```python if u not in dist: dist[u] = {} dist[u][v] = cost[i] n = len(source) res = 0 for i in range(n): if source[i]!= target[i]: u = ord(source[i]) - ord('a') v = ord(target[i]) - ord('a') res += memoization(u, v) return res if res < INF else -1 ``` |
class Solution(object): def minimumCost(self, source, target, original, changed, cost): """ :type source: str :type target: str :type original: List[str] :type changed: List[str] :type cost: List[int] :rtype: int """ INF = float("inf") def dijkstra(start): best = {start:0} min_heap = [(0, start)] while min_heap: curr, u = heapq.heappop(min_heap) if curr > best[u]: continue if u not in dist: continue for v, w in dist[u].items(): if v in best and best[v] <= curr+w: continue best[v] = curr+w heapq.heappush(min_heap, (best[v], v)) return best memo = {} def memoization(u, v): if u not in memo: memo[u] = dijkstra(u) return memo[u][v] if v in memo[u] else INF dist = {} for i in range(len(original)): u, v = ord(original[i])-ord('a'), ord(changed[i])-ord('a') if u not in dist: dist[u] = {v:INF} if v not in dist[u]: dist[u][v] = INF dist[u][v] = min(dist[u][v], cost[i]) result = sum(memoization(ord(source[i])-ord('a'), ord(target[i])-ord('a')) for i in range(len(source))) return result if result != INF else -1 |
| gpto1 | ```python if u not in dist: dist[u] = {} dist[u][v] = min(dist[u].get(v, INF), cost[i]) total_cost = 0 for i in range(len(source)): u = ord(source[i]) - ord('a') v = ord(target[i]) - ord('a') if u == v: continue c = memoization(u, v) if c == INF: return -1 total_cost += c return total_cost ``` |
class Solution(object): def minimumCost(self, source, target, original, changed, cost): """ :type source: str :type target: str :type original: List[str] :type changed: List[str] :type cost: List[int] :rtype: int """ INF = float("inf") def dijkstra(start): best = {start:0} min_heap = [(0, start)] while min_heap: curr, u = heapq.heappop(min_heap) if curr > best[u]: continue if u not in dist: continue for v, w in dist[u].items(): if v in best and best[v] <= curr+w: continue best[v] = curr+w heapq.heappush(min_heap, (best[v], v)) return best memo = {} def memoization(u, v): if u not in memo: memo[u] = dijkstra(u) return memo[u][v] if v in memo[u] else INF dist = {} for i in range(len(original)): u, v = ord(original[i])-ord('a'), ord(changed[i])-ord('a') if u not in dist: dist[u] = {v:INF} if v not in dist[u]: dist[u][v] = INF dist[u][v] = min(dist[u][v], cost[i]) result = sum(memoization(ord(source[i])-ord('a'), ord(target[i])-ord('a')) for i in range(len(source))) return result if result != INF else -1 |
| kimi | ```python dist[chr(u+ord('a'))] = dist.get(chr(u+ord('a')), {}) dist[chr(u+ord('a'))][chr(v+ord('a'))] = cost[i] total_cost = 0 for i in range(len(source)): if source[i] != target[i]: if source[i] in dist and target[i] in dist[source[i]]: total_cost += memoization(ord(source[i])-ord('a'), ord(target[i])-ord('a')) else: return -1 return total_cost if total_cost < INF else -1 ``` |
class Solution(object): def minimumCost(self, source, target, original, changed, cost): """ :type source: str :type target: str :type original: List[str] :type changed: List[str] :type cost: List[int] :rtype: int """ INF = float("inf") def dijkstra(start): best = {start:0} min_heap = [(0, start)] while min_heap: curr, u = heapq.heappop(min_heap) if curr > best[u]: continue if u not in dist: continue for v, w in dist[u].items(): if v in best and best[v] <= curr+w: continue best[v] = curr+w heapq.heappush(min_heap, (best[v], v)) return best memo = {} def memoization(u, v): if u not in memo: memo[u] = dijkstra(u) return memo[u][v] if v in memo[u] else INF dist = {} for i in range(len(original)): u, v = ord(original[i])-ord('a'), ord(changed[i])-ord('a') if u not in dist: dist[u] = {v:INF} if v not in dist[u]: dist[u][v] = INF dist[u][v] = min(dist[u][v], cost[i]) result = sum(memoization(ord(source[i])-ord('a'), ord(target[i])-ord('a')) for i in range(len(source))) return result if result != INF else -1 |
| minimax | ```python class Solution(object): def minimumCost(self, source, target, original, changed, cost): """ :type source: str :type target: str :type original: List[str] :type changed: List[str] :type cost: List[int] :rtype: int """ INF = float("inf") def dijkstra(start): best = {start: 0} min_heap = [(0, start)] while min_heap: curr, u = heapq.heappop(min_heap) if curr > best[u]: continue if u not in dist: continue for v, w in dist[u].items(): if v in best and best[v] <= curr + w: continue best[v] = curr + w heapq.heappush(min_heap, (best[v], v)) return best memo = {} def memoization(u, v): if u not in memo: memo[u] = dijkstra(u) return memo[u][v] if v in memo[u] else INF dist = {} for i in range(len(original)): u, v |
class Solution(object): def minimumCost(self, source, target, original, changed, cost): """ :type source: str :type target: str :type original: List[str] :type changed: List[str] :type cost: List[int] :rtype: int """ INF = float("inf") def dijkstra(start): best = {start:0} min_heap = [(0, start)] while min_heap: curr, u = heapq.heappop(min_heap) if curr > best[u]: continue if u not in dist: continue for v, w in dist[u].items(): if v in best and best[v] <= curr+w: continue best[v] = curr+w heapq.heappush(min_heap, (best[v], v)) return best memo = {} def memoization(u, v): if u not in memo: memo[u] = dijkstra(u) return memo[u][v] if v in memo[u] else INF dist = {} for i in range(len(original)): u, v = ord(original[i])-ord('a'), ord(changed[i])-ord('a') if u not in dist: dist[u] = {v:INF} if v not in dist[u]: dist[u][v] = INF dist[u][v] = min(dist[u][v], cost[i]) result = sum(memoization(ord(source[i])-ord('a'), ord(target[i])-ord('a')) for i in range(len(source))) return result if result != INF else -1 |
| wenxinyiyan | ```python if u not in dist: dist[u] = {} dist[u][v] = cost[i] if v not in dist: dist[v] = {} dist[v][u] = cost[i] total_cost = 0 n = len(source) for i in range(n): u, v = ord(source[i])-ord('a'), ord(target[i])-ord('a') if u == v: continue cost_uv = memoization(u, v) if cost_uv == INF: return -1 total_cost += cost_uv return total_cost ``` In this solution, we first define a helper function `dijkstra` that implements Dijkstra's algorithm to find the shortest path from a given start node to all other nodes in the graph. The graph is represented by the `dist` dictionary, where each key is a node and the corresponding value is a dictionary mapping adjacent nodes to their respective edge weights. Next, we define another helper function `memoization` that uses memoization to cache the results of the Dijkstra algorithm for each node. This helps to avoid redundant calculations and improve the efficiency of the solution. In the main part of the code, we iterate over the `original`, `changed`, and `cost` arrays to construct the graph represented by the `dist` dictionary. For each index `i`, we retrieve the characters `u` and `v` corresponding to `original[i]` and `changed[i]`, respectively. We then add an edge from `u` to `v` with weight `cost[i]` and vice versa. After constructing the graph, we initialize the `total_cost` variable to 0 and iterate over the characters of `source` and `target` simultaneously. For each index `i`, we retrieve the characters `u` and `v` corresponding to `source[i]` and `target[i]`, respectively. If `u` and `v` are the same, we continue to the next iteration since no conversion is needed. Otherwise, we calculate the minimum cost `cost_uv` to convert `u` to `v` using the `memoization` function. If `cost_uv` is infinite (indicating that no path exists from `u` to `v`), we return -1 since it is impossible to convert `source` to `target`. Otherwise, we add `cost_uv` to the `total_cost` variable. Finally, we return the `total_cost` as the minimum cost to convert `source` to `target`. |
class Solution(object): def minimumCost(self, source, target, original, changed, cost): """ :type source: str :type target: str :type original: List[str] :type changed: List[str] :type cost: List[int] :rtype: int """ INF = float("inf") def dijkstra(start): best = {start:0} min_heap = [(0, start)] while min_heap: curr, u = heapq.heappop(min_heap) if curr > best[u]: continue if u not in dist: continue for v, w in dist[u].items(): if v in best and best[v] <= curr+w: continue best[v] = curr+w heapq.heappush(min_heap, (best[v], v)) return best memo = {} def memoization(u, v): if u not in memo: memo[u] = dijkstra(u) return memo[u][v] if v in memo[u] else INF dist = {} for i in range(len(original)): u, v = ord(original[i])-ord('a'), ord(changed[i])-ord('a') if u not in dist: dist[u] = {v:INF} if v not in dist[u]: dist[u][v] = INF dist[u][v] = min(dist[u][v], cost[i]) result = sum(memoization(ord(source[i])-ord('a'), ord(target[i])-ord('a')) for i in range(len(source))) return result if result != INF else -1 |
| zhipu | ``` if u not in dist: dist[u] = {} if v in dist[u]: dist[u][v] = min(dist[u][v], cost[i]) else: dist[u][v] = cost[i] total_cost = 0 for i in range(len(source)): s, t = ord(source[i]) - ord('a'), ord(target[i]) - ord('a') if s == t: continue cost = memoization(s, t) if cost == INF: return -1 total_cost += cost return total_cost ``` |
class Solution(object): def minimumCost(self, source, target, original, changed, cost): """ :type source: str :type target: str :type original: List[str] :type changed: List[str] :type cost: List[int] :rtype: int """ INF = float("inf") def dijkstra(start): best = {start:0} min_heap = [(0, start)] while min_heap: curr, u = heapq.heappop(min_heap) if curr > best[u]: continue if u not in dist: continue for v, w in dist[u].items(): if v in best and best[v] <= curr+w: continue best[v] = curr+w heapq.heappush(min_heap, (best[v], v)) return best memo = {} def memoization(u, v): if u not in memo: memo[u] = dijkstra(u) return memo[u][v] if v in memo[u] else INF dist = {} for i in range(len(original)): u, v = ord(original[i])-ord('a'), ord(changed[i])-ord('a') if u not in dist: dist[u] = {v:INF} if v not in dist[u]: dist[u][v] = INF dist[u][v] = min(dist[u][v], cost[i]) result = sum(memoization(ord(source[i])-ord('a'), ord(target[i])-ord('a')) for i in range(len(source))) return result if result != INF else -1 |
| deepseek-v3 | ```python if u not in dist: dist[u] = {} if v not in dist[u] or cost[i] < dist[u][v]: dist[u][v] = cost[i] total_cost = 0 for i in range(len(source)): s_char = ord(source[i]) - ord('a') t_char = ord(target[i]) - ord('a') if s_char == t_char: continue if s_char not in memo: memo[s_char] = dijkstra(s_char) if t_char not in memo[s_char]: return -1 total_cost += memo[s_char][t_char] return total_cost ``` |
class Solution(object): def minimumCost(self, source, target, original, changed, cost): """ :type source: str :type target: str :type original: List[str] :type changed: List[str] :type cost: List[int] :rtype: int """ INF = float("inf") def dijkstra(start): best = {start:0} min_heap = [(0, start)] while min_heap: curr, u = heapq.heappop(min_heap) if curr > best[u]: continue if u not in dist: continue for v, w in dist[u].items(): if v in best and best[v] <= curr+w: continue best[v] = curr+w heapq.heappush(min_heap, (best[v], v)) return best memo = {} def memoization(u, v): if u not in memo: memo[u] = dijkstra(u) return memo[u][v] if v in memo[u] else INF dist = {} for i in range(len(original)): u, v = ord(original[i])-ord('a'), ord(changed[i])-ord('a') if u not in dist: dist[u] = {v:INF} if v not in dist[u]: dist[u][v] = INF dist[u][v] = min(dist[u][v], cost[i]) result = sum(memoization(ord(source[i])-ord('a'), ord(target[i])-ord('a')) for i in range(len(source))) return result if result != INF else -1 |