You are given a Weighted Directed Acyclic Graph (DAG) consisting of ‘N’ nodes and ‘E’ directed edges. Nodes are numbered from 0 to ’N’-1. You are also given a source node ‘Src’ in it. Your task is to find the longest distances from ‘Src’ to all the nodes in the given graph.
Return an array of ‘N’ integers where ‘ith’ integer gives the maximum distance of the node ‘i’ from the source node ‘Src’.
A Directed Acyclic Graph (DAG) is a directed graph that contains no cycles.
Note:
Print -1 if a node is not reachable from the given source node.
Example:
Consider the following DAG consists of 5 nodes and 7 edges, Let the source node ‘Src’ be 0.

Then the maximum distance of node 0 from the source node 0 is 0. (the distance of a node from itself is always 0).
The maximum distance of node 1 from the source node 0 is 3. The path that gives this maximum distance is 0 -> 1.
The maximum distance of node 2 from the source node 0 is 10. The path that gives this maximum distance is 0 -> 2.
The maximum distance of node 3 from the source node 0 is 15. The path that gives this maximum distance is 0 -> 2 -> 3.
The maximum distance of node 4 from the source node 0 is 54. The path that gives this maximum distance is 0 -> 1 -> 4.
Thus we should print 0 3 10 15 54
The first line of input contains an integer ‘T’ denoting the number of test cases. then ‘T’ test cases follow.
The first line of each test case consists of three space-separated integers ‘N’, ‘E’, and ‘Src’ representing the number of nodes, number of edges, and the given source node in the given DAG respectively.
The next ‘E’ lines consist of three space-separated integers ‘U’, ‘V’, ‘W’ representing that there is a directed edge from node U to V having weight ‘W’.
Output format :
For each test case, print ‘N’ space-separated integers where ’ith’ integer gives the maximum distance of node ‘i’ from the source node ‘Src’.
The output of each test case will be printed in a new line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 50
1 <= N <= 10^4
0 <= E <= 10^4
0 <= Src <= N-1
0 <= U, V <= N-1
1 <= W <= 10^5
Where ‘T’ is the total number of test cases, ‘N’, ‘E’, and ‘Src’ representing the number of nodes, number of edges, and the given source node in the given DAG respectively. ‘U’, ‘V’, ‘W’ represents that there is a directed edge from node U to V having weight ‘W’.
Time limit: 1 sec
2
1 0 0
5 7 0
0 1 3
0 2 10
0 3 14
1 3 7
1 4 51
2 3 5
3 4 11
0
0 3 10 15 54
Test case 1:
There is only one node, which is also the source node since the distance from the source node to itself is 0, so we will print only a single integer 0.
Test case 2:
See the problem statement for an explanation.
2
5 4 2
0 1 1
0 2 1
1 2 1
3 4 1
5 4 0
0 1 1
0 2 1
1 2 1
3 4 1
-1 -1 0 -1 -1
0 1 2 -1 -1
Try out all possible paths starting from the source node.
First we create an integer array ‘maxDistances’ of size ‘N’ and fill it with -1, maxDistances[i] will give the maximum distance of node ‘i’ from the given source node. We will then recursively try out all paths starting from the source node. We will also use an integer variable ‘dist’ to track distance covered so far on this path. Whenever we reaches any node ‘i’ we update its maximum distance i.e maxDistances[i] with maximum of its current value and ‘dist’.
Algorithm
O(N^N), where ‘N’ is the number of nodes in the given directed graph.
For each node there are at most ‘N’ vertices that can be visited from the current node and there are ‘N’ nodes in the graph. Thus, overall it will take O(N^N) time to explore all the paths.
O(N + E), where ‘N’ is the number of nodes in the given directed graph and E is the number of edges.
The space used by the adjacency list ‘adj’ is of the order of O(N + E), the space used by array ‘maxDistances’ is of order O(N) and the space used by recursion stack is of order O(N). Thus, the final space complexity will be O(N+E) + O(N)+ O(N) = O(N+E).