Job at Todechef Problem Code: JOBCHF

 

Preface:

There is no need for introduction of uses of shortest path algorithms in real life. Used intensively in industrial applications like GPS, finding shortest path has been of interest to many researchers.

However, the most important, yet undervalued aspect for shortest path algorithm is the graph itself. Often, due to some special properties of the graph needed in our application, we can make certain assumptions  which would otherwise be wrong in general. This opens up a new world of optimizing the graph algorithms because certain invalid algorithms might also produce correct results because of the earlier assumptions…

Statement:

In a far away Galaxy of Tilky Way, there was a planet Tarth where the sport of Tompetitive Toding was very popular. According to legends, there lived a setter who gave out interview questions asked at Todechef and Tirecti.

Tima and Tuchika were giving their interview for the role of developers at Todechef. Their interviewers, Ms Teha, Ms Tamesh and Ms Takkoth were die hard fans of selfies, food and graph algorithms. Hence, they asked Tima and Tuchika to find the shortest path in the given un-directed graph. They said that this graph has a very special property - The edges will have weight of EITHER k1 OR k2.

Tima, who was too busy dating, to study properly for interview, said that she would apply 01 BFS on it by taking the minimum weight to correspond to weight 0 and maximum weight to correspond to weight 1 in the algorithm. Tuchika, who studied very very hard to impress her crush, said that she'd apply Bellman Ford algorithm to it and report the answer directly.

Ms Teha, Ms Tamesh and Ms Takkoth were not happy. They expected better answers for this job with salary of 1018 Todechef Kaddus per month. Hence, now its upto you to make both Tima and Tuchika realize their mistake!!

You need to report 3 things - the correct answer, and the absolute differences between correct answer and answers given by Tima and Tuchika respectively. You need to report this assuming starting node to be St and taking every node as target node one by one.

(Note- If multiple edges lead to some node V, then Tima will visit all the edges, and see which one gives minimum distance. This is the core step of 01 BFS)

Input:

  • The first line contains 3 integers - N M St , i.e. number of nodes, number of edges and starting node respectively.
  • Next M lines contain 3 integers u v and W denoting that there is an edge between node u and node v of weight W

Output:

Output N lines  where ith line denotes answer if node i was the target node. Each line should be containing 3 integers- Ac D1 and D2 - where Ac is the correct answer for shortest path, D1 is absolute difference between correct answer and Tima's answer, and D2 is similarly absolute difference between Tuchika's and correct answer.

Constraints

  • 1N105
  • 1M1.5105
  • 1St,u,vN
  • 0W109
  • W will have at most 2 distinct values in the graph. It is very much possible that k1=k2, making the graph have only 1 distinct value.

Note- Input can be huge. Prefer fast IO methods.

Subtasks:

  • 5% points - Given graph is a tree.
  • 8% points - N100
  • 8% points - One of the weights is 0
  • 9% points - N5000
  • 70% points - Original Constraints

Sample Input:

2 1 1
1 2 5

Sample Output:

0 0 0
5 0 0

EXPLANATION:

We see that it is a simple graph with node 1 connected to node 2 by a simple edge of weight 5. We can verify that the shortest path from node 1 to node 2 must use this edge, and hence be of weight 5. Also, for this particular graph, both algorithms give correct answer.

(Note- Distance of a node to itself is always 0).

NOTES:

The 01 BFS algorithm can be

for all v in vertices:
    dist[v] = inf
dist[source] = 0;
deque d
d.push_front(source)
while d.empty() == false:
    vertex = get front element and pop as in BFS.
    for all edges e of form (vertex , u):
        if travelling e shortens distance to u:
            update(dist[u]);
            if e.weight = 1:
                d.push_back(u)
            else:
                d.push_front(u)
Click here
Previous
Next Post »