88 952
Assignments Done
97.2%
Successfully Done
In July 2020

# Answer to Question #42148 in C++ for liora

Question #42148
5733 - Graph_single source all destination

Time limit: 10s
Memory limit: 32MB
Problem Description

Given a directed graph with nonnegative edge costs, we want to compute a shortest path from a source vertex to each of the other vertices, The cost is 0 if source equals to destination. The vertex index is from 1 to n. You can use any algorithm but note that you should bound your program in O(n^2) to prevent time limit exceed error.

Input

The input consists of three parts.

First part is the 1 line: &lt;#vertices &gt; &lt;#edges&gt;
Second part is begin from 2 line to #edges+1:&lt;Source ID&gt; &lt; Destination ID&gt; &lt; Edge Weight&gt;
Third part is source vertex you need to compute.

P.S. The testcase range is from 1 vertex to 5000 vertices

Output

Please list the shortest path form the source vertex to all(except itself) vertex.
If the path is ∞ ,please use “INF” to represent,and show “No path from &lt;source ID&gt; to &lt;destination ID&gt;”.
If the shortest path is more than one , when you choose the shortest edge from each vertex, adjacent vertex with smaller ID should be prioritized within the same edge weight,there is an example for you to understand.

Sample Input

6 11
1 2 50
1 3 45
1 4 10
2 3 10
2 4 15
3 5 30
4 1 20
4 5 15
5 2 20
5 3 35
6 5 3
2
Sample Output

2..4..1..-&gt;35
2..3..-&gt;10
2..4..-&gt;15
2..4..5..-&gt;30
No path from 2 to 6-&gt;INF
0

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!