Advertisement
Guest User

myuglycode

a guest
Mar 22nd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4.  
  5. #define INF INT_MAX
  6.  
  7. using namespace std;
  8.  
  9. void Floyd(int **graph, int verticesCount)
  10. {
  11.     for (int k = 0; k < verticesCount; k++)
  12.     {
  13.         for (int i = 0; i < verticesCount; i++)
  14.         {
  15.             for (int j = 0; j < verticesCount; j++)
  16.             {
  17.                 if (graph[i][k] < INF && graph[k][j] < INF)
  18.                     graph[i][j]= min(graph[i][j], graph[i][k] + graph[k][j]);
  19.             }
  20.         }
  21.     }
  22. }
  23.  
  24. int minimalPath(int from, int to, int **graph, int **tempGraph, int verticesCount)
  25. {
  26.     if (from == to) return 0;
  27.     for (int i=0; i< verticesCount; i++)
  28.         if (graph[from - 1][i] + tempGraph[i][to - 1] == graph[from - 1][to - 1])
  29.         {
  30.             if (from - 1 != i && to - 1 != i)
  31.                 return i+1;
  32.         }
  33. }
  34. void inicializeGraph(ifstream input)
  35. {
  36.     input.open ("E:\\input.txt");
  37.     int verticesCount;
  38.     int from, to;
  39.     input >> verticesCount;
  40.     int ** graph = new int* [verticesCount];
  41.  
  42.     for (int i = 0; i < verticesCount; i++)
  43.     {
  44.         graph[i] = new int [verticesCount];
  45.     }
  46.     int ** tempGraph = new int* [verticesCount];
  47.  
  48.     for (int i = 0; i < verticesCount; i++)
  49.     {
  50.         tempGraph[i] = new int [verticesCount];
  51.     }
  52.  
  53.     for (int i = 0; i < verticesCount; i++)
  54.     {
  55.         for (int j = 0; j < verticesCount; j++)
  56.         {
  57.             input >> graph[i][j];
  58.             if (graph[i][j] == 9000000) graph[i][j] = INT_MAX;
  59.             tempGraph[i][j] = graph[i][j];
  60.         }
  61.     }
  62.  
  63.     input >> from >> to;
  64.     input.close();
  65. }
  66.  
  67.  
  68. void printGraph(int **graph, int verticesCount)
  69. {
  70.     for ( int i=0; i< verticesCount; i++)
  71.     {
  72.         for (int j=0; j< verticesCount; j++)
  73.         {
  74.             if (graph[i][j] == INF)
  75.             {
  76.                 cout << "~" << " ";
  77.             }
  78.             else
  79.             {
  80.                 cout << graph[i][j] << " ";
  81.             }
  82.         }
  83.         cout << "\n";
  84.     }
  85. }
  86. void main()
  87. {
  88.     setlocale(LC_ALL, "rus");
  89.     ifstream input("E:\\input.txt");
  90.     ofstream output;
  91.    
  92.     int verticesCount;
  93.     int from, to;
  94.     input >> verticesCount;
  95.     int ** graph = new int* [verticesCount];
  96.  
  97.     for (int i = 0; i < verticesCount; i++)
  98.     {
  99.         graph[i] = new int [verticesCount];
  100.     }
  101.     int ** tempGraph = new int* [verticesCount];
  102.  
  103.     for (int i = 0; i < verticesCount; i++)
  104.     {
  105.         tempGraph[i] = new int [verticesCount];
  106.     }
  107.  
  108.     for (int i = 0; i < verticesCount; i++)
  109.     {
  110.         for (int j = 0; j < verticesCount; j++)
  111.         {
  112.             input >> graph[i][j];
  113.             if (graph[i][j] == 9000000) graph[i][j] = INT_MAX;
  114.             tempGraph[i][j] = graph[i][j];
  115.         }
  116.     }
  117.  
  118.     input >> from >> to;
  119.     input.close();
  120.     cout << "Начальный граф "<< endl;
  121.     printGraph(graph, verticesCount);
  122.  
  123.     Floyd(graph, verticesCount);
  124.  
  125.     cout << "Граф после применения алгоритма Флойда "<< endl;
  126.     printGraph(graph, verticesCount);
  127.  
  128.     cout << "Кратчайший путь между двумя заданными вершинами"<< endl;
  129.     cout << from <<" " << minimalPath(from, to, graph, tempGraph, verticesCount) <<" "<< to;
  130.  
  131.     output.open("E:\\output.txt");
  132.     for (int i = 0; i < verticesCount; i++)
  133.     {
  134.         for (int j = 0; j < verticesCount; j++)
  135.         {
  136.             if (graph[i][j] == INF)
  137.             {
  138.                 output << "~" << " ";
  139.             }
  140.             else
  141.             {
  142.                 output << graph[i][j] << " ";
  143.             }
  144.         }
  145.         output << endl;
  146.     }
  147.     output << "Кратчайший путь между двумя заданными вершинами"<< endl;
  148.     output << from <<" " << minimalPath(from, to, graph, tempGraph, verticesCount) <<" "<< to << endl;
  149.  
  150.     output.close();
  151.     system("pause");
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement