java - Floyd Warshall Algorithm for Shortest Path -
I was looking through some old competition questions, and I found this one, it looked fun, Trying to use Warshal algorithm to get the least path from any node to any node, can you see what I did wrong? This contest does not give the desired output set on the question page
import java.io. *; Import java.util. *; Public class distance throws fileNotFoundException (scanner S = new scanner) (new file ("DATA5.txt")); Net N = integer. ; Int [] [] for dist = new int [60] [60]; for (int y = 0; y & lt; 60; ++ y) (int x = 0; x & lt; 60; + + X) dist [y] [x] = 10000000; map & lt; character, integer & gt; map = new tree mark & lt; character, integer & gt; (); for (int i = 0; i & lt ; N; ++ i) {string text [] = s.nextLine (). Partition (""); int c = 0; if (! Map.contai NsKey (text [0] .charAt (0)) {map.put (text [0] .charAt (0), c); c ++;} if (! Map.containsKey (text [0] .charAt ( 1))) {map.put (text [0] .charAt (1), c); c ++;} Dist [map.get (text [0] .charAt (0))] [map.get (text [0] .charAt (1))] = integer parasont (text [1]);} for (int k = 0; k
There are several problems with your code:
Hyperbolic mapping
is your int c local code for which means that the highest used mapping index does not last until the next recurrence, therefore The previous one overrides by reading in the next trip, so the distance matrix is not filled properly after the data is loaded. Solution: int c = 0; Unidirectional roads Unidirectional roads
are bidirectional in the directions of the roads, but you only register them as unidirectional towns between non-
Solution: add
In addition to these difficult issues, I also have couple signals for you. You do not follow them, but as if come If you want to improve your programming skills, then you should think about them.
Messi code
Your code is hard to read, many information such as indicies, solving It is not difficult to read such code, but it is very difficult for debug and OK . I recommend you write this a cleaner. Elgo Ithm efficiency
A O (n ^ 3) is complexity problems of size (number of towns) A-M = 13. In this complexity, it repeats 13 ^ 3 = 2197 . I know, it may not appear to be too much, but consider the amount of work to be resolved in a certain time frame. I'll give you the complexity O (| E | + | V | log | V |) . The worst case with some simplification in this work is | E = (| V | ^ 2) / 2, | Wei | 13 = . It means the last number of iterations 5 (| V | ^ 2/2 + | V | log | V |) | = 5 (13 ^ 2/2 + 13 * log13) ~ 5 * 132 = 660 < / Code>. If I am not wrong and have not made any mistake, then it is very low, especially when we consider total actions. Reading the input
I could be wrong but I did not force many programming competitions and competitions and this ever present people to work with files, an input always files Was redirected to a standard input from I think that its main reason is a security, but simplification is probably very beneficial.
Comments
Post a Comment