Advertisement
PhotoShaman

Laba15

Mar 1st, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <clocale>
  4. #include <windows.h>
  5. #include <iomanip>
  6. #include <conio.h>
  7.  
  8. using namespace std;
  9.  
  10. int IntInput() // Проверка на Int
  11. {
  12.     int imputNumber;
  13.     while (!(cin >> imputNumber) || (cin.peek() != '\n'))
  14.     {
  15.         cin.clear();
  16.         while (cin.get() != '\n');
  17.         cout << "Это не целое число. Попробуйте еще раз.\n";
  18.     }
  19.     return imputNumber;
  20. }
  21.  
  22. void main()
  23. {
  24.     SetConsoleCP(1251);
  25.     SetConsoleOutputCP(1251);
  26.  
  27.     cout << "Введите размерность графа: ";
  28.     int N;
  29.     do
  30.     {
  31.         N = IntInput();
  32.         if (N < 1 || N > 21)
  33.         {
  34.             cout << "Число вне допустимых пределов! Повторите ввод числа.";
  35.         }
  36.         else break;
  37.     } while (true);
  38.  
  39.     typedef int *pInt;
  40.     pInt *W;
  41.     //выделение памяти под динамическую матрицу
  42.     W = new pInt[N];
  43.     for (int i = 0; i < N; i++)
  44.         W[i] = new int[N];
  45.  
  46.     string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //соответствие именам вершин графа
  47.  
  48.     cout << "Заполните матрицу связей гарфа (отсутствие связи = 0): " << endl;
  49.     cin.clear();
  50.     cin.sync();
  51.     for (int i = 0; i < N; i++)
  52.     {
  53.         for (int j = 0; j < i; j++)
  54.         {
  55.             cout << "Ребро " << str[i] << " - " << str[j] << " : ";
  56.             cin >> W[i][j];
  57.             if (W[i][j] == 0)
  58.                 W[i][j] = 999;
  59.         }
  60.         for (int j = 0; j < N; j++)
  61.             if (W[i][j] > 100 || W[i][j] < 0)
  62.                 W[i][j] = 999;
  63.     }
  64.  
  65.     //вывод на экран
  66.     cout << endl << "Весовая матрица:" << endl;
  67.     cout << endl << " ";
  68.     for (int i = 0; i < N; i++)
  69.         cout << "   " << str[i];
  70.     cout << endl << " ";
  71.     for (int i = 0; i < N * 4 + 1; i++)
  72.         cout << "-";
  73.     cout << endl;
  74.     for (int i = 0; i < N; i++)
  75.     {
  76.         cout << str[i] << " |";
  77.         for (int j = 0; j < N; j++)
  78.             if (W[i][j] == 999)
  79.                 cout << setw(3) << " " << "|";
  80.             else
  81.                 cout << setw(3) << W[i][j] << "|";
  82.         cout << endl << " ";
  83.         for (int q = 0; q < N * 4 + 1; q++)
  84.             cout << "-";
  85.         cout << endl;
  86.     }
  87.     // конец вывода на экран  
  88.     int *col = new int[N];
  89.     for (int i = 0; i < N; i++) col[i] = i;
  90.     typedef int *Pint;
  91.     Pint *ostov = new Pint[N - 1];
  92.     for (int i = 0; i < N - 1; i++)
  93.         ostov[i] = new int[2];
  94.  
  95.     int k, iMin, jMin, minDist, c;
  96.  
  97.     for (k = 0; k < N - 1; k++)
  98.     {
  99.         // поиск ребра с минимальным весом
  100.         minDist = 99999;
  101.         for (int i = 0; i < N; i++)
  102.             for (int j = 0; j < N; j++)
  103.                 if (col[i] != col[j] && W[i][j] < minDist)
  104.                 {
  105.                     iMin = i; jMin = j;
  106.                     minDist = W[i][j];
  107.                 }
  108.         // добавление ребра в список выбранных
  109.         ostov[k][0] = iMin;
  110.         ostov[k][1] = jMin;
  111.         // перекрашивание вершин
  112.         c = col[jMin];
  113.         for (int i = 0; i < N; i++)
  114.             if (col[i] == c)
  115.                 col[i] = col[iMin];
  116.     }
  117.  
  118.     //Результат
  119.     for (int i = 0; i < N - 1; i++)
  120.         cout << "(" << str[ostov[i][0]] << "," << str[ostov[i][1]] << ")" << endl;
  121.  
  122.     // очистка памяти
  123.     for (int i = 0; i < N; i++)
  124.         delete[] W[i];
  125.     delete[] W;
  126.     cout << endl << "---Выход - Esc.";
  127.     while (!GetAsyncKeyState(VK_ESCAPE));
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement