Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. // Floyd–Warshall.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <vector>
  6. #include <iostream>
  7. #include <set>
  8. #include <algorithm>
  9. using namespace std;
  10.  
  11. int main()
  12. {
  13.  
  14. int n;
  15.  
  16. freopen("input.txt", "rt", stdin);
  17. freopen("output.txt", "wt", stdout);
  18.  
  19. cin >> n;
  20.  
  21. vector<vector<int>> graph(n);
  22.  
  23. for (int i = 0; i < n; i++){
  24. graph[i] = vector<int>(n);
  25. for (int j = 0; j < n; j++){
  26. cin >> graph[i][j];
  27. }
  28. }
  29.  
  30. for (int k = 0; k < n; ++k) {
  31. for (int i = 0; i < n; ++i) {
  32. for (int j = 0; j < n; ++j) {
  33. if (graph[i][k] != -1 && graph[k][j] != -1) {
  34. if (graph[i][j] != -1) {
  35. graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j]);
  36. }
  37. else {
  38. graph[i][j] = graph[i][k] + graph[k][j];
  39. }
  40. }
  41. }
  42. }
  43. }
  44. int _max = 0;
  45. for (int i = 0; i < n; i++) {
  46. for (int j = 0; j < n; j++) {
  47. _max = max(graph[i][j], _max);
  48. }
  49. }
  50.  
  51. cout << _max;
  52.  
  53. return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement