Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8. const int MaxVert = 5000;
  9. ifstream input("input.txt");
  10. ofstream output("output.txt");
  11. int N = 0, M = 0;
  12. int u, v, w;
  13. input >> N;
  14. input >> M;
  15.  
  16. vector<vector<int>> matrix (N, vector<int>(N, MaxVert));
  17. for (int i = 0; i < M; i++) {
  18. input >> u;
  19. input >> v;
  20. input >> w;
  21. matrix[u - 1][v - 1] = w;
  22. }
  23.  
  24. for (int k = 0; k < N; k++) {
  25. for (int i = 0; i < N; i++) {
  26. for (int j = 0; j < N; j++) {
  27. if ((matrix[i][k] < MaxVert) && (matrix[k][j] < MaxVert)) {
  28. matrix[i][j] = min(matrix[i][j], matrix[i][k] + matrix[k][j]);
  29. }
  30. }
  31. }
  32. }
  33.  
  34. for (int i = 0; i < N; i++) {
  35. for (int j = 0; j < N; j++) {
  36. if (i == j) {
  37. output << 0;
  38. } else if (matrix[i][j] != MaxVert) {
  39. output << matrix[i][j];
  40. } else {
  41. output << " ";
  42. }
  43.  
  44. if (j < N - 1) {
  45. output << " ";
  46. }
  47. }
  48. if (i < N - 1) {
  49. output << "\n";
  50. }
  51. }
  52. return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement