16:19 ~/318 > cat fw.cpp #include #include int smin(int a, int b) { if (a == -1) return b; if (b == -1) return a; if (a < b) return a; return b; } int splus(int a, int b) { if (a == -1) return -1; if (b == -1) return -1; return a + b; } typedef int mat5[5][5]; void step(int i, mat5 prev, mat5 next) { for (int r = 0; r < 5; r += 1) for (int c = 0; c < 5; c += 1) next[r][c] = smin(prev[r][c], splus(prev[r][i], prev[i][c])); } void display(int i, mat5 di) { cout << "D" << i << ":\n"; for (int r = 0; r < 5; r += 1) { for (int c = 0; c < 5; c += 1) cout << setw(3) << di[r][c]; cout << "\n"; } cout << "\n\n"; } int main() { mat5 d0 = { { 0, 1, 3, -1, -1 }, { 1, 0, 1, -1, 7 }, { 3, 1, 0, 1, 3 }, { -1, -1, 1, 0, 1 }, { -1, 7, 3, 1, 0 } }; mat5 d1, d2, d3, d4, d5; step(0, d0, d1); step(1, d1, d2); step(2, d2, d3); step(3, d3, d4); step(4, d4, d5); display(0, d0); display(1, d1); display(2, d2); display(3, d3); display(4, d4); display(5, d5); } 16:19 ~/318 > CC fw.cpp 16:19 ~/318 > a.out D0: 0 1 3 -1 -1 1 0 1 -1 7 3 1 0 1 3 -1 -1 1 0 1 -1 7 3 1 0 D1: 0 1 3 -1 -1 1 0 1 -1 7 3 1 0 1 3 -1 -1 1 0 1 -1 7 3 1 0 D2: 0 1 2 -1 8 1 0 1 -1 7 2 1 0 1 3 -1 -1 1 0 1 8 7 3 1 0 D3: 0 1 2 3 5 1 0 1 2 4 2 1 0 1 3 3 2 1 0 1 5 4 3 1 0 D4: 0 1 2 3 4 1 0 1 2 3 2 1 0 1 2 3 2 1 0 1 4 3 2 1 0 D5: 0 1 2 3 4 1 0 1 2 3 2 1 0 1 2 3 2 1 0 1 4 3 2 1 0