vadimk772336

Untitled

Nov 19th, 2021
1,303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. struct agent
  7. {
  8.     int x;
  9.     int y;
  10. };
  11.  
  12. double dist(agent A, agent B)
  13. {
  14.     int x = A.x - B.x;
  15.     int y = A.y - B.y;
  16.     return sqrt(x * x + y * y);
  17. }
  18.  
  19. int main()
  20. {
  21.     int n,x, y;
  22.     cin >> n;
  23.     vector<struct agent> agents(n);
  24.  
  25.     for (int i = 0; i < n; ++i)
  26.     {
  27.         cin >> agents[i].x;
  28.         cin >> agents[i].y;
  29.     }
  30.  
  31.     vector<vector<double> > g(n, vector<double>(n));
  32.     for (int i = 0; i < n; ++i)
  33.     {
  34.         for (int j = i + 1; j < n; ++j)
  35.         {
  36.             double d = dist(agents[i], agents[j]);
  37.             g[i][j] = d;
  38.             g[j][i] = d;
  39.             //cout << "d= " << d << " "<<  i <<" " <<  j <<  endl;
  40.         }
  41.     }
  42.  
  43. /*
  44.     for (int i = 0; i < n; ++i) {
  45.         for (int j = 0; j < n; ++j) {
  46.             cout << g[i][j] << " ";
  47.         }
  48.         cout << endl;
  49.     }
  50. */
  51.  
  52.     const int INF = 1000000001; // значение "бесконечность"
  53.  
  54.  
  55.  
  56.     vector<bool> used(n); //означает, что вершина i включена в остов
  57.     vector<double> min_e(n, INF); //хранит вес наименьшего допустимого ребра из вершины в остов
  58.  
  59.     min_e[0] = 0;
  60.     double R = 0;
  61.     for (int i = 0; i < n; ++i) //делаем всего n шагов
  62.     {
  63.         int v = -1;
  64.  
  65.         //чтобы выборрать миним ребро, надо просмотреть эти минимальные рёбра у каждой не выбранной
  66.         //ещё вершины
  67.         for (int j = 0; j < n; ++j)
  68.             if (!used[j] && (v == -1 || min_e[j] < min_e[v])) //выбирает вершину v с наименьшей меткой
  69.                 v = j; // помечает её
  70.  
  71.         if (R < min_e[v])
  72.             R = min_e[v];
  73.         cout << "curr_R = " << R << endl;
  74.        
  75.         cout << "v= " << v << " min_e = " << min_e[v] <<  endl;
  76.         used[v] = true;
  77.         for (int to = 0; to < n; ++to) { //просматривает все рёбра из этой вершины, пересчитывая их метки.
  78.             cout << "g[v][to] = " << g[v][to] << endl;
  79.             if (g[v][to] < min_e[to]) { // g[i][j] - расстоение между агентами i j
  80.                 min_e[to] = g[v][to];
  81.                 cout << "min_e " << min_e[to] << endl;
  82.             }
  83.         }
  84.     }
  85.  
  86.     for (int i = 1; i < n; ++i)
  87.         cout << min_e[i] << " ";
  88.         //if (min_e[0] > min_e[i])
  89.             //min_e[0] = min_e[i];
  90.  
  91.     cout << "R = " << min_e[0];
  92.  
  93.     return 0;
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment