Pattern: 3 QS
You are given an undirected connected graph with N nodes and M edges. The nodes are numbered from 0 to N-1.
Each edge connects two nodes and normally has a weight of 1.
You are also given:
SVYour task is to find the minimum distance from the source node S to every other node.
Whenever a path reaches the special node V, the cost of reaching V becomes 0.
Moreover, after reaching V, every node that is directly connected to V can also be reached with zero additional cost.
In other words, the special node acts as a zero-cost node: moving to V costs 0, and moving from V to any of its adjacent nodes also costs 0.
Return an array dist where dist[i] represents the minimum cost required to reach node i from the source node S.
N, the number of nodes.MM lines contain two integers u and v, representing an undirected edge between nodes u and v.S, the source node.V, the special node.Print N integers where the i-th integer represents the minimum distance from S to node i.
5
5
0 1
1 2
2 3
3 4
1 3
2
0
0 1 1 1 2
The source node is 2 and the special node is 0.
The normal shortest distances from node 2 are:
dist[2] = 0dist[1] = 1dist[3] = 1dist[0] = 2dist[4] = 2Therefore, the resulting distance array is:
0 1 1 1 2
Expedia • Pending
Expedia • Pending
Teradata • Pending
Teradata • Pending