Advertisement
kej

Dejkstra

kej
May 12th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.33 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. #include<cmath>
  4. #include <stdio.h>
  5. using namespace std;
  6. struct graph
  7. {
  8.     int x;
  9.     int y;
  10.     int weight;
  11. };
  12. struct node
  13. {
  14.     graph G;
  15.     node* next;
  16. };
  17. void push (node* &p, graph G)
  18. {
  19.     node *temp;
  20.     temp = new node;
  21.     temp->G = G;
  22.     temp->next =p;
  23.     p = temp;
  24.    
  25.    
  26. }
  27. void show (node* &p)
  28. {
  29.     while (p)
  30.     {
  31.         cout<<p->G.x <<" "<< p->G.y << " "<<p->G.weight<<endl;
  32.                       p = p->next;
  33.     }
  34. }
  35. void read (node* &p,ifstream &file)
  36. {
  37.     p = NULL;
  38.     graph g;
  39.     while (file >> g.x >>g.y >> g.weight)
  40.     {
  41.         push(p, g);
  42.     }
  43. }
  44. void write (node* p,ofstream &out)
  45. {
  46.     while (p)
  47.     {
  48.         out<<p->G.x <<" "<< p->G.y << " "<<p->G.weight<<endl;
  49.                p = p->next;
  50.     }
  51. }
  52.  
  53. void show_matrix (ofstream &fout, int **matrix,int N)
  54. {
  55.     for (int i=0;i<N;i++)
  56.               {
  57.                  
  58.               for (int j=0;j<N;j++)
  59.                   {
  60.                       fout<<matrix[i][j]<<" ";
  61.                   }
  62.                   fout<<endl;
  63.               }
  64. }
  65. void read_matrix (ifstream &fin, int matrix[6][6],int N)
  66. {
  67.    {
  68.     for (int i=0;i<N;i++)
  69.         {
  70.         for (int j=0;j<N;j++)
  71.             {
  72.                 fin>>matrix[i][j];
  73.             }
  74.         }
  75.     }
  76. }
  77.  
  78. void search (node *p, int n)
  79. {
  80.     node* tmp = p;
  81.     int counter = 0;
  82.     while (p)
  83.     {
  84.         if (tmp->G.x == n|| tmp->G.y ==n)
  85.         {
  86.             counter++;
  87.         }
  88.         tmp = tmp->next;
  89.     }
  90.     cout<<counter;
  91. }
  92. void opway (int matrix[6][6], int N)
  93. {
  94.     int minW[N];
  95.     int checked[N];
  96.     int temp, minindex, min;
  97.     int start_index = 0;
  98.     for (int i = 0; i<N; i++)
  99.     {
  100.       for (int j = 0; j<N; j++)
  101.         cout<<matrix[i][j]<<" ";
  102.         cout<<endl;
  103.     }
  104.    
  105.     for (int i = 0; i<N; i++)
  106.     {
  107.       minW[i] = 10000;
  108.       checked[i] = 1;
  109.     }
  110.     minW[start_index] = 0;
  111.     while (minindex < 10000) {
  112.       minindex = 10000;
  113.       min = 10000;
  114.       for (int i = 0; i<N; i++)
  115.       {
  116.         if ((checked[i] == 1) && (minW[i]<min))
  117.         {
  118.           min = minW[i];
  119.           minindex = i;
  120.         }
  121.       }
  122.      
  123.       if (minindex != 10000)
  124.       {
  125.         for (int i = 0; i<N; i++)
  126.         {
  127.           if (matrix[minindex][i] > 0)
  128.           {
  129.             temp = min + matrix[minindex][i];
  130.             if (temp < minW[i])
  131.             {
  132.               minW[i] = temp;
  133.             }
  134.           }
  135.         }
  136.         checked[minindex] = 0;
  137.       }
  138.     }
  139.  
  140.     int cchecked[N];
  141.     int end = 4;
  142.     cchecked[0] = end + 1;
  143.     int k = 1;
  144.     int weight = minW[end];
  145.  
  146.     while (end != start_index)
  147.     {
  148.       for (int i = 0; i<N; i++)
  149.         if (matrix[i][end] != 0)
  150.         {
  151.           int temp = weight - matrix[i][end];
  152.           if (temp == minW[i])
  153.           {
  154.             weight = temp;
  155.             end = i;
  156.             cchecked[k] = i + 1;
  157.             k++;
  158.           }
  159.         }
  160.     }
  161.     cout<<endl<<"Кратчайший путь: "<<endl;
  162.     for (int i=k-1;i>=0;i--)
  163.     {
  164.         cout<<cchecked[i]<<" ";
  165.     }
  166.     cout<<endl;
  167. }
  168. int main()
  169. {
  170.     ifstream fin ("input.txt");
  171.     ofstream fout ("output.txt");
  172.     int N = 6;
  173.     int matrix[6][6];
  174.     read_matrix(fin,matrix,N);
  175.     opway(matrix, N);
  176.     return 0;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement