Skip to content

Commit d86a046

Browse files
committed
Optimize Simplex
This won at least a factor 3 in performance when testing on Road Times, ICPC 2016 (at the cost of slightly decreased precision).
1 parent 053195f commit d86a046

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

code/Simplex.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@ struct LPSolver {
4444
}
4545

4646
void Pivot(int r, int s) {
47+
double inv = 1.0 / D[r][s];
4748
for (int i = 0; i < m + 2; i++) if (i != r)
4849
for (int j = 0; j < n + 2; j++) if (j != s)
49-
D[i][j] -= D[r][j] * D[i][s] / D[r][s];
50-
for (int j = 0; j < n + 2; j++) if (j != s) D[r][j] /= D[r][s];
51-
for (int i = 0; i < m + 2; i++) if (i != r) D[i][s] /= -D[r][s];
52-
D[r][s] = 1.0 / D[r][s];
50+
D[i][j] -= D[r][j] * D[i][s] * inv;
51+
for (int j = 0; j < n + 2; j++) if (j != s) D[r][j] *= inv;
52+
for (int i = 0; i < m + 2; i++) if (i != r) D[i][s] *= -inv;
53+
D[r][s] = inv;
5354
swap(B[r], N[s]);
5455
}
5556

0 commit comments

Comments
 (0)