<p>You are given an <strong>undirected weighted</strong> <strong>connected</strong> graph containing <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and an integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, w<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> with weight <code>w<sub>i</sub></code>.</p>

<p>Some edges have a weight of <code>-1</code> (<code>w<sub>i</sub> = -1</code>), while others have a <strong>positive</strong> weight (<code>w<sub>i</sub> &gt; 0</code>).</p>

<p>Your task is to modify <strong>all edges</strong> with a weight of <code>-1</code> by assigning them <strong>positive integer values </strong>in the range <code>[1, 2 * 10<sup>9</sup>]</code> so that the <strong>shortest distance</strong> between the nodes <code>source</code> and <code>destination</code> becomes equal to an integer <code>target</code>. If there are <strong>multiple</strong> <strong>modifications</strong> that make the shortest distance between <code>source</code> and <code>destination</code> equal to <code>target</code>, any of them will be considered correct.</p>

<p>Return <em>an array containing all edges (even unmodified ones) in any order if it is possible to make the shortest distance from </em><code>source</code><em> to </em><code>destination</code><em> equal to </em><code>target</code><em>, or an <strong>empty array</strong> if it&#39;s impossible.</em></p>

<p><strong>Note:</strong> You are not allowed to modify the weights of edges with initial positive weights.</p>

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

<p><strong class="example"><img alt="" src="https://assets.leetcode.com/uploads/2023/04/18/graph.png" style="width: 300px; height: 300px;" /></strong></p>

<pre>
<strong>Input:</strong> n = 5, edges = [[4,1,-1],[2,0,-1],[0,3,-1],[4,3,-1]], source = 0, destination = 1, target = 5
<strong>Output:</strong> [[4,1,1],[2,0,1],[0,3,3],[4,3,1]]
<strong>Explanation:</strong> The graph above shows a possible modification to the edges, making the distance from 0 to 1 equal to 5.
</pre>

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

<p><strong class="example"><img alt="" src="https://assets.leetcode.com/uploads/2023/04/18/graph-2.png" style="width: 300px; height: 300px;" /></strong></p>

<pre>
<strong>Input:</strong> n = 3, edges = [[0,1,-1],[0,2,5]], source = 0, destination = 2, target = 6
<strong>Output:</strong> []
<strong>Explanation:</strong> The graph above contains the initial edges. It is not possible to make the distance from 0 to 2 equal to 6 by modifying the edge with weight -1. So, an empty array is returned.
</pre>

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

<p><strong class="example"><img alt="" src="https://assets.leetcode.com/uploads/2023/04/19/graph-3.png" style="width: 300px; height: 300px;" /></strong></p>

<pre>
<strong>Input:</strong> n = 4, edges = [[1,0,4],[1,2,3],[2,3,5],[0,3,-1]], source = 0, destination = 2, target = 6
<strong>Output:</strong> [[1,0,4],[1,2,3],[2,3,5],[0,3,1]]
<strong>Explanation:</strong> The graph above shows a modified graph having the shortest distance from 0 to 2 as 6.
</pre>

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

<ul>
	<li><code>1 &lt;= n &lt;= 100</code></li>
	<li><code><font face="monospace">1 &lt;= edges.length &lt;= n * (n - 1) / 2</font></code></li>
	<li><code>edges[i].length == 3</code></li>
	<li><code>0 &lt;= a<sub>i</sub>, b<sub>i&nbsp;</sub>&lt;&nbsp;n</code></li>
	<li><code><font face="monospace">w<sub>i</sub>&nbsp;= -1&nbsp;</font></code>or <code><font face="monospace">1 &lt;= w<sub>i&nbsp;</sub>&lt;= 10<sup><span style="font-size: 10.8333px;">7</span></sup></font></code></li>
	<li><code>a<sub>i&nbsp;</sub>!=&nbsp;b<sub>i</sub></code></li>
	<li><code>0 &lt;= source, destination &lt; n</code></li>
	<li><code>source != destination</code></li>
	<li><code><font face="monospace">1 &lt;= target &lt;= 10<sup>9</sup></font></code></li>
	<li>The graph is connected, and there are no self-loops or repeated edges</li>
</ul>

from collections import defaultdict
from typing import List
import heapq

class Solution:
    def modifiedGraphEdges(self, n: int, edges: List[List[int]], source: int, destination: int, target: int) -> List[List[int]]:
        adj_matrix = defaultdict(lambda: defaultdict(int))
        modify_index = []
        NO_CONNECT = target + 1

        # 將 edges 轉換成 adj_matrix, 可以 modify 的 edges 先設定 weight = 1
        for i, edge in enumerate(edges):
            [ai, bi, wi] = edge
            if wi == -1:
                modify_index.append(i)
                wi = 1
            adj_matrix[ai][bi] = adj_matrix[bi][ai] = wi

        def dijkstra(adj_matrix, source, n) -> List[int]:
            distances = [float('inf')] * n
            distances[source] = 0
            priority_queue = [(0, source)]
            
            while priority_queue:
                current_distance, current_node = heapq.heappop(priority_queue)
                
                if current_distance > distances[current_node]:
                    continue
                
                for neighbor, weight in adj_matrix[current_node].items():
                    distance = current_distance + weight
                    if distance < distances[neighbor]:
                        distances[neighbor] = distance
                        heapq.heappush(priority_queue, (distance, neighbor))
            
            return distances

        # 1. 前面已經設定 weight = 1，如果最短路徑還是大於 target，則無解
        d_src = dijkstra(adj_matrix, source, n)
        if d_src[destination] > target:
            return []

        # 2. 如果設定 weight = NO_CONNECT, 然後最短路徑還是小於 target，則無解
        for i in modify_index:
            [ai, bi, wi] = edges[i]
            edges[i][2] = adj_matrix[ai][bi] = adj_matrix[bi][ai] = NO_CONNECT

        d_src = dijkstra(adj_matrix, source, n)
        if d_src[destination] < target:
            return []

        # 3. 如果設定 weight = NO_CONNECT, 然後最短路徑還是等於 target，表示現在的狀況就是解
        if d_src[destination] == target:
            return edges

        # 4. 其他的情況，都會有解
        # 參考 Hint: 
        #     假設我們能找到一條可修改的邊 `(u, v)`，使得從起點到 `u` 的最短路徑長度 (`dis1`) 加上
        #     從 `v` 到終點的最短路徑長度 (`dis2`) 小於目標值 (`dis1 + dis2 < target`)，
        #     那麼我們可以將這條邊的權重設為 `target - dis1 - dis2`。

        # 每一個可以更改的點，都先設定為 1
        for i in modify_index:
            [ai, bi, wi] = edges[i]
            edges[i][2] = adj_matrix[ai][bi] = adj_matrix[bi][ai] = 1

            # 如果最短距離已經比 target 小，還需要將 weight 調高
            d_src = dijkstra(adj_matrix, source, n)
            d_dst = dijkstra(adj_matrix, destination, n)

            # print(f'd_src[destination]={d_src[destination]}, target={target}')
            if d_src[destination] < target:
                # 需要考慮 a->b 以及 b->a 兩個方向
                diff = target - min(d_src[ai] + d_dst[bi], d_src[bi] + d_dst[ai])
                # print(f'src-a={d_src[ai]}, b-dst={d_dst[bi]}, diff={diff}')
                # print(f'src-a={d_src[bi]}, b-dst={d_dst[ai]}, diff={diff}')

                # 重新檢查一下，最短路徑是否真的變成 target
                edges[i][2] = adj_matrix[ai][bi] = adj_matrix[bi][ai] = diff
                d_src = dijkstra(adj_matrix, source, n)
                if d_src[destination] == target:
                    break
                # 我不確定會不會發生，還是寫著放在比較安全
                elif d_src[destination] < target:
                    edges[i][2] = adj_matrix[ai][bi] = adj_matrix[bi][ai] = NO_CONNECT

        return edges

5704