fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. vector<vector<int>> fw_correct(vector<vector<int>> W) {
  5. int n = (int)W.size();
  6. for (int b = 0; b < n; ++b)
  7. for (int a = 0; a < n; ++a)
  8. for (int c = 0; c < n; ++c)
  9. W[a][c] = min(W[a][c], W[a][b]+W[b][c]);
  10. return W;
  11. }
  12.  
  13. vector<vector<int>> fw_wrong(vector<vector<int>> W) {
  14. int n = (int)W.size();
  15. for (int a = 0; a < n; ++a)
  16. for (int c = 0; c < n; ++c)
  17. for (int b = 0; b < n; ++b)
  18. W[a][c] = min(W[a][c], W[a][b]+W[b][c]);
  19. return W;
  20. }
  21.  
  22. void print(const vector<vector<int>> &W) {
  23. int n = (int)W.size();
  24. for (int r = 0; r < n; ++r) {
  25. cout << '\t';
  26. for (int c = 0; c < n; ++c)
  27. cout << W[r][c] << "\t\n"[c+1==n];
  28. }
  29. }
  30.  
  31. int main() {
  32. vector<vector<int>> W(4, vector<int>(4, 99));
  33. W[0][2] = W[2][0] = 1;
  34. W[2][3] = W[3][2] = 1;
  35. W[3][1] = W[1][3] = 1;
  36.  
  37. cout << "Correct:" << endl;
  38. print(fw_correct(W));
  39. cout << "Wrong:" << endl;
  40. print(fw_wrong(W));
  41. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Correct:
	2	3	1	2
	3	2	2	1
	1	2	2	1
	2	1	1	2
Wrong:
	2	99	1	2
	99	2	2	1
	1	2	2	1
	2	1	1	2