So i have been trying to get Unidirectional TSP working using java.
my DP algorithim first marks the distance to every cell in the matrix using a column first order. as i discover a cell I decide which of the three possible approaches to that cell would be best. marking the total distance and the parent(s). After the whole matrix has been discovered, I find the best possible end points.
I now travel backwards for each column. I find the best distances that are reachable (i can get to a possibility the next column) in each column, and i mark them as possibilities and keep going to the previous columns.
Now i have marked all cells which lie upon a best possible best path.
Lexicagraphically means dictionary order right? so i want the first column to have the lowest possible number, and so forth, giving preference to earlier column. So that an ordering or possibile paths is
1543
3333
4321
assuming they are all valid paths with an equal distance that is the minimal for the matrix.
we should return 1543 right?
i start with nothing commited. so i travel along the valid cells matrix, starting at the first column, for each column.
I take the lowest numbered cell that is reachable(I can get to it from the previously commited cell), and i commit that cell to my list.
in path 0=not in path, 1 = in path
0 1 0 0
1 0 0 0
0 0 0 0
1 1 1 1
0 0 1 1
And my program reports a path
2 1 5 4
with weight=4
I initially failed the test inputs in this thread. My code did not produce the first lexi ordered one. I then rewrote my discovery process (to what i described above). I now solve all of the input in the thread almost instantly.
My code still gave WA, so i tried rewriting my input method, to not be concerned with reading lines.
It then gave me a TLE, and after tuning my new input method, I got a WA after 9.5 seconds. Anybody have any ideas, any new input, any suspected problems in my algorithim, any help?
Be carefully when you must go from first row to last and from last to first ... espesially if weight of paths are the same
Maybe this help you
DM
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)
i didnt check your code, since i'm in internet cafe now, but have you try matrix with dimension 1 X N, 2 X N, N X 1, N X 2? i think that's the special case in this problem. and my ac prog didnt use sorthing, only DP.
I also got WA with my code in PASCAL, but after I translated it to C language, I got AC.
I don't know why, cause I use the same algorithm, but the OJ gives different result.