Find edges in shortest path(leetcode hard)

 You are given an undirected weighted graph of n nodes numbered from 0 to n - 1. The graph consists of m edges represented by a 2D array edges, where edges[i] = [ai, bi, wi] indicates that there is an edge between nodes ai and bi with weight wi.

Consider all the shortest paths from node 0 to node n - 1 in the graph. You need to find a boolean array answer where answer[i] is true if the edge edges[i] is part of at least one shortest path. Otherwise, answer[i] is false.

Return the array answer.

Note that the graph may not be connected.

 

Example 1:

Input: n = 6, edges = [[0,1,4],[0,2,1],[1,3,2],[1,4,3],[1,5,1],[2,3,1],[3,5,3],[4,5,2]]
Output: [true,true,true,false,true,true,true,false]


My Solution:

Store a map for every node containing all the path from starting node to this node. Naturally we will have all path from source to n-1 as well. this map is prepared while performing Dikstra algorithm.

535/536 tests passed (Memory limit exceeded)

Other Solution:

2 dikstra. from 0 to all and from n-1 to all.
now for each edge(a,b) check if dist(0->a)+weight(a,b)+dist(b->n-1) = shortest distance(0->n-1) then this edge is in shortest path route.

Comments

Popular posts from this blog

LeetCode 3Sum

New start Day 2 (1/10/2020) codeforces 492B Vanya and Lantern

Heap (min)