private void OptimizedMethod() { // Current min long[] currMin = new long[Lines]; int[][] pathLines = new int[Lines][]; for (int i = 0; i < Lines; i++) { pathLines[i] = new int[Nodes]; } for (int i = 0; i < Nodes; i++) { pathLines[1][i] = 1; } Enter.CopyTo(currMin, 0); currMin[0] += StationCost[0, 0]; currMin[1] += StationCost[1, 0]; for (int i = 1; i < Nodes; i++) { long[] next = new long[Lines]; for (int j = 0; j < Lines; j++) { long sameLine = StationCost[j, i] + currMin[j]; long switchLine = StationCost[j, i] + currMin[(j + 1) % Lines] + SwitchCost[(j + 1) % Lines, i - 1]; if (sameLine > switchLine) { next[j] = switchLine; pathLines[j][i] = (j + 1) % Lines; } else { next[j] = sameLine; } } for (int j = 0; j < Lines; j++) { currMin[j] = next[j]; } } currMin[0] += Exit[0]; currMin[1] += Exit[1]; if (currMin[0] < currMin[1]) { MinTime = currMin[0]; Path = pathLines[0]; } else { MinTime = currMin[1]; Path = pathLines[1]; } }