SHARE
TWEET

Untitled

a guest Dec 14th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3.  
  4.  
  5. class Graph {
  6. private:
  7.  
  8.     std::vector<std::vector<int>> data;
  9.  
  10. public:
  11.  
  12.     Graph(size_t N);
  13.  
  14.     ~Graph(){}
  15.  
  16.     void AddEdge(size_t from, size_t to, int weight);
  17.  
  18.     void Floyd();
  19.  
  20. };
  21.  
  22.  
  23. Graph::Graph(size_t N) {
  24.     for (size_t i = 0; i < N; ++i) {
  25.         std::vector<int> tmp(N, 0);
  26.         data.push_back(tmp);
  27.     }
  28. }
  29.  
  30.  
  31. void Graph::AddEdge(size_t from, size_t to, int weight) {
  32.     data.at(from).at(to) = weight;
  33. }
  34.  
  35.  
  36. void Graph::Floyd() {
  37.     for (size_t k = 0; k < data.size(); ++k)
  38.         for (size_t i = 0; i < data.size(); ++i)
  39.             for (size_t j = 0; j < data.size(); ++j)
  40.                 data.at(i).at(j) = (data.at(i).at(j) > data.at(i).at(k) + data.at(k).at(j)) ? data.at(i).at(k) + data.at(k).at(j) : data.at(i).at(j);
  41.     for (size_t i = 0; i < data.size(); ++i) {
  42.         for (size_t j = 0; j < data.size(); ++j)
  43.             std::cout << data.at(i).at(j) << ' ';
  44.         std::cout << std::endl;
  45.     }
  46.  
  47. }
  48.  
  49.  
  50. int main()
  51. {
  52.     size_t N;
  53.     std::cin >> N;
  54.    
  55.     Graph current{ N };
  56.  
  57.     for(size_t i = 0; i < N; ++i)
  58.         for (size_t j = 0; j < N; ++j) {
  59.             int weight;
  60.             std::cin >> weight;
  61.             current.AddEdge(i, j, weight);
  62.         }
  63.  
  64.     current.Floyd();
  65.  
  66.     system("pause");
  67.     return 0;
  68. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top