B. One Way Streets
Author: Suhendry Effendy
Prepared By: Suhendry Effendy, Felix Halim
(link to problem B)
If you have learned about graph algorithm, then this problem is easy for you.
This problem can be solved simply by any shortest path algorithms, e.g., Dijkstra, Bellman-Ford, or Floyd Warshall. Notice that reversing an edge is the same as having a cost to traverse the edge in opposite direction. So, we can construct the graph where traversing an edge in its direction cost 0, while traversing its opposite direction cost 1.
Floyd Warshall (FW) is a smart choice (even though Dijkstra is okay) as the number of nodes is quite small (≤ 100). However, you should be careful if you want to use FW, especially when constructing the cost matrix. Recall that there might be multi-edges (same pair of nodes connected by multiple edges) in the input. One directed edge in the input contributes to two directed edges in the graph: one with cost 0 in the same direction, and the other with cost 1 in the opposite direction. In the cost matrix, you should store only the one with minimum cost. There might be edges in both directions (A to B and B to A) in the input, in which the cost matrix for cost[A][B] and cost[B][A] both should be 0.
There are 124 submissions for this problem in which 49 managed to solve this problem. The first team who solved this problem is BerinGAS (minute 18) from University of Indonesia.