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

import heapq
from typing import List

MAX = 2 * 10**9


class Solution:
    # INF = int(2e9)

    # def modifiedGraphEdges(
    #     self,
    #     n: int,
    #     edges: List[List[int]],
    #     source: int,
    #     destination: int,
    #     target: int,
    # ) -> List[List[int]]:
    #     # Step 1: Compute the initial shortest distance from source to destination
    #     current_shortest_distance = self.run_dijkstra(
    #         edges, n, source, destination
    #     )

    #     # If the current shortest distance is less than the target, return an empty result
    #     if current_shortest_distance < target:
    #         return []
    #     matches_target = current_shortest_distance == target

    #     # Step 2: Iterate through each edge to adjust its weight if necessary
    #     for edge in edges:
    #         # Skip edges that already have a positive weight
    #         if edge[2] > 0:
    #             continue

    #         # Set edge weight to a large value if current distance matches target, else set to 1
    #         edge[2] = self.INF if matches_target else 1

    #         # Step 3: If current shortest distance does not match target
    #         if not matches_target:
    #             # Compute the new shortest distance with the updated edge weight
    #             new_distance = self.run_dijkstra(edges, n, source, destination)
    #             # If the new distance is within the target range, update edge weight to match target
    #             if new_distance <= target:
    #                 matches_target = True
    #                 edge[2] += target - new_distance

    #     # Return modified edges if the target distance is achieved, otherwise return an empty result
    #     return edges if matches_target else []

    # def run_dijkstra(
    #     self, edges: List[List[int]], n: int, source: int, destination: int
    # ) -> int:
    #     # Step 1: Initialize adjacency matrix and distance arrays
    #     adj_matrix = [[self.INF] * n for _ in range(n)]
    #     min_distance = [self.INF] * n
    #     visited = [False] * n

    #     # Set the distance to the source node as 0
    #     min_distance[source] = 0

    #     # Step 2: Fill the adjacency matrix with edge weights
    #     for nodeA, nodeB, weight in edges:
    #         if weight != -1:
    #             adj_matrix[nodeA][nodeB] = weight
    #             adj_matrix[nodeB][nodeA] = weight

    #     # Step 3: Perform Dijkstra's algorithm
    #     for _ in range(n):
    #         # Find the nearest unvisited node
    #         nearest_unvisited_node = -1
    #         for i in range(n):
    #             if not visited[i] and (
    #                 nearest_unvisited_node == -1
    #                 or min_distance[i] < min_distance[nearest_unvisited_node]
    #             ):
    #                 nearest_unvisited_node = i

    #         # Mark the nearest node as visited
    #         visited[nearest_unvisited_node] = True

    #         # Update the minimum distance for each adjacent node
    #         for v in range(n):
    #             min_distance[v] = min(
    #                 min_distance[v],
    #                 min_distance[nearest_unvisited_node]
    #                 + adj_matrix[nearest_unvisited_node][v],
    #             )
    #     # Return the shortest distance to the destination node
    #     return min_distance[destination]


    def get_distance(self, i, edges):
        if edges[i][2] > 0:
            return edges[i][2]
        return MAX
    def run_dijkstra(self, source, destination, adj, edges):
        h = [(0, source)]
        dists = {}
        min_dists = [float('inf') for _ in range(len(adj))]
        while h:
            min_dist, node = heapq.heappop(h)
            if node in dists:
                continue
            if node == destination:
                return min_dist
            dists[node] = min_dist
            for next_node, edge_ind in adj[node]:
                if next_node in dists:
                    continue
                new_min_dist = self.get_distance(edge_ind, edges) + min_dist
                if min_dists[next_node] > new_min_dist:
                    min_dists[next_node] = new_min_dist
                    heapq.heappush(h, (new_min_dist, next_node))

    def modifiedGraphEdges(self, n: int, edges: List[List[int]], source: int, destination: int, target: int) -> List[List[int]]:
        adj = [[] for _ in range(n)]
        for i, (u, v, w) in enumerate(edges):
            adj[u].append((v, i))
            adj[v].append((u, i))
        min_dist = self.run_dijkstra(source, destination, adj, edges)
        if min_dist < target:
            return []
        meet_target = min_dist == target
        for edge in edges:
            if edge[2] > 0:
                continue
            if meet_target:
                edge[2] = MAX
            else:
                edge[2] = 1
                new_dist = self.run_dijkstra(source, destination, adj, edges)
                if new_dist <= target:
                    edge[2] += target - new_dist
                    meet_target = True
        if not meet_target:
            return []
        return edges






        # edge2ind = {}
        # adj = [[] for _ in range(n)]
        # for i, (u, v, w) in enumerate(edges):
        #     adj[u].append(v)
        #     adj[v].append(u)
        #     edge2ind[(u, v)] = i
        # paths, paths_sum, n_neg_edges = [], [], []
        # self.dfs(source, target, adj, edge2ind, paths, paths_sum, n_neg_edges)
        # all_pos_paths_sum = [s for s in zip(paths_sum, n_neg_edges) if n_neg == 0)]
        # if all_pos_paths_sum and min(all_pos_paths_sum) < target:
        #     return []
        # if all_pos_paths_sum and min(all_pos_path_sum) == target:
        #     for edge in edges:
        #         u, v, w = edge
        #         if w == -1:
        #             edge[2] = MAX
        #     return edges
        # if min(paths_sum) > target:
        #     return []
        # for path, path_sum, n_neg in zip(paths, paths_sum, n_neg_edges):
        #     if path_sum + n_neg < target:
        #         for e_ind in path:
        #             if edges[e_ind][2] == -1:
        #                 if n_neg > 1:
        #                     edges[e_ind][2] = 1
        #                 else:
        #                     edges[e_ind][2] = target - path_sum - n_neg + 1
        #         for edge in edges:
        #             u, v, w = edge
        #             if w == -1:
        #                 edge[2] = MAX
        #         return edges
        # return []

        
5976