Job at Todechef Problem Code: ISITSP
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 OR .
Tima, who was too busy dating, to study properly for interview, said that she would apply on it by taking the minimum weight to correspond to weight and maximum weight to correspond to weight in the algorithm. Tuchika, who studied very very hard to impress her crush, said that she'd apply 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 Todechef Kaddus per month. Hence, now its upto you to make both Tima and Tuchika realize their mistake!!
You need to report 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 and taking every node as target node one by one.
(Note- If multiple edges lead to some node , then Tima will visit all the edges, and see which one gives minimum distance. This is the core step of )
Input:
- The first line contains integers - , i.e. number of nodes, number of edges and starting node respectively.
- Next lines contain integers and denoting that there is an edge between node and node of weight
Output:
Output lines where line denotes answer if node was the target node. Each line should be containing integers- and - where is the correct answer for shortest path, is absolute difference between correct answer and Tima's answer, and is similarly absolute difference between Tuchika's and correct answer.
Constraints
- will have at most distinct values in the graph. It is very much possible that , making the graph have only distinct value.
Note- Input can be huge. Prefer fast IO methods.
Subtasks:
- 5% points - Given graph is a tree.
- 8% points -
- 8% points - One of the weights is
- 9% points -
- 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 connected to node by a simple edge of weight . We can verify that the shortest path from node to node must use this edge, and hence be of weight . Also, for this particular graph, both algorithms give correct answer.
(Note- Distance of a node to itself is always ).
NOTES:
The 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)
Comments
Post a Comment