Skip to content

2699. Modify Graph Edge Weights #436

Answered by topugit
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We can break down the approach as follows:

Approach:

  1. Initial Check with Existing Weights:

    • First, we compute the shortest path from source to destination using only the edges with positive weights, ignoring the edges with weight -1.
    • If this distance is already greater than target, then it's impossible to modify the -1 edges to achieve the target, so we return an empty array.
  2. Temporary Assignment of Weight 1:

    • Next, assign a temporary weight of 1 to all edges with weight -1 and recompute the shortest path.
    • If this shortest path is still greater than target, then it's impossible to achieve the target, so we return an empty array.
  3. Modify Specific Edge Weights:

    • Iterate through the ed…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@mah-shamim
Comment options

mah-shamim Aug 30, 2024
Maintainer Author

@topugit
Comment options

topugit Aug 30, 2024
Collaborator

Answer selected by mah-shamim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested hard Difficulty
2 participants