RMarK0

Курсач адм обновление

May 30th, 2021
841
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include "stdafx.h"
  3. #include <iostream>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <conio.h>
  8.  
  9. using namespace std;
  10. #define word unsigned int
  11. int i, j, n, p, xn, xk;
  12. int flag[11];
  13. word c[11][11], l[11];
  14. char s[80], path[80][11];
  15.  
  16. int min(int n)
  17. {
  18.     int i, result;
  19.     for (i = 0; i<n; i++)
  20.         if (!(flag[i])) result = i;
  21.  
  22.     for (i = 0; i<n; i++)
  23.         if ((l[result]>l[i]) && (!flag[i])) result = i;
  24.  
  25.     return result;
  26. }
  27.  
  28. word minim(word x, word y)
  29. {
  30.     if (x<y) return x;
  31.     return y;
  32. }
  33.  
  34. void main()
  35. {
  36.     system("chcp 1251");
  37.     system("pause");
  38.  
  39.     cout << "Введите кол-во точек: ";
  40.     cin >> n;
  41.     for (i = 0; i<n; i++)
  42.         for (j = 0; j<n; j++) c[i][j] = 0;
  43.     for (i = 0; i<n; i++)
  44.         for (j = i + 1; j<n; j++)
  45.         {
  46.             cout << "Введите расстояние от " << i + 1 << " до " << j + 1 << ": ";
  47.             cin >> c[i][j];
  48.         }
  49.     cout << endl << endl;
  50.     for (i = 0; i<n; i++)
  51.     {
  52.         for (j = 0; j<n; j++)
  53.         {
  54.             printf("%6d", c[i][j]);
  55.             c[j][i] = c[i][j];
  56.         }
  57.         printf("\n\n");
  58.     }
  59.  
  60.     for (i = 0; i<n; i++)
  61.         for (j = 0; j<n; j++)
  62.             if (c[i][j] == 0) c[i][j] = UINT32_MAX; //бесконечность
  63.  
  64.     cout << "Введите начальную точку: ";
  65.     cin >> xn;
  66.     cout << "Введите конечную точку: ";
  67.     cin >> xk;
  68.     xk--;
  69.     xn--;
  70.  
  71.     if (xn == xk)
  72.     {
  73.         cout << "Начальная и конечная точки совпадают" << endl;
  74.         _getch();
  75.         return;
  76.     }
  77.  
  78.     for (i = 0; i<n; i++)
  79.     {
  80.         flag[i] = 0;
  81.         l[i] = UINT32_MAX;
  82.     }
  83.  
  84.     l[xn] = 0;
  85.     flag[xn] = 1;
  86.     p = xn;
  87.     _itoa_s(xn + 1, s, 10);
  88.  
  89.     for (i = 1; i <= n; i++)
  90.     {
  91.         strcpy_s(path[i], "");
  92.         strcat_s(path[i], s);
  93.     }
  94.  
  95.     do
  96.     {
  97.         for (i = 0; i<n; i++)
  98.             if ((c[p][i] != UINT32_MAX) && (!flag[i]) && (i != p))
  99.             {
  100.                 if (l[i]>l[p] + c[p][i])
  101.                 {
  102.                     _itoa_s(i + 1, s, 10);
  103.                     strcpy_s(path[i + 1], path[p + 1]);
  104.                     strcat_s(path[i + 1], " ");
  105.                     strcat_s(path[i + 1], s);
  106.                 }
  107.                 l[i] = minim(l[i], l[p] + c[p][i]);
  108.             }
  109.  
  110.         p = min(n);
  111.         flag[p] = 1;
  112.     }
  113.     while (p != xk);
  114.  
  115.     if (l[p] != UINT32_MAX)
  116.     {
  117.         cout << "Путь: " << path[p + 1] << endl;
  118.         cout << "Длина пути: " << l[p] << endl;
  119.     }
  120.     else
  121.         cout << "Такого пути не существует" << endl;
  122.  
  123.     _getch();
  124. }
Advertisement
Add Comment
Please, Sign In to add comment