Advertisement
allia

пираты

Jan 8th, 2021
823
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. #include<iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. const int inf = 100000;
  7.  
  8. struct coordinata
  9. {
  10.  double x, y, rad;
  11.  void new_coord(double x, double y, double rad)
  12.     {
  13.         this->x = x;
  14.         this->y = y;
  15.         this->rad = rad;
  16.     }
  17. };
  18.  
  19. double path_length(coordinata a, coordinata b)
  20. {
  21.   double dlina = 0;
  22.   dlina = sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y)) - a.rad - b.rad;
  23.   return dlina;
  24. }
  25.  
  26. class Graph
  27. {
  28.   private:
  29.   int n;
  30.   double **arr, **d;
  31.   public:
  32.   Graph (coordinata *matrix, int x)
  33.   {
  34.     n = x;
  35.  
  36.     arr = new double*[n];
  37.     for (int i =0; i < n; i++)
  38.      arr[i] = new double[n];
  39.  
  40.     for (int i = 0; i < n; i++)
  41.      for (int j = 0; j < n; j++)
  42.        {
  43.          arr[i][j] = path_length(matrix[i], matrix[j]);
  44.          if (i == j)
  45.          arr[i][j] = inf;
  46.        }
  47.   }
  48.   void get_rasst();
  49.   void minpath();
  50.   double get_length_path(int x, int y);
  51. };
  52.  
  53. void Graph::minpath()
  54. {
  55.   for (int l = 0; l < n; l++)
  56.     for (int i = 0; i < n; i++)
  57.       if (d[i][l] < inf)
  58.         for (int j = 0; j < n; j++)
  59.           d[i][j] = min(d[i][j], d[i][l] + d[l][j]);
  60. }
  61.  
  62. double Graph::get_length_path(int x, int y)
  63. {
  64.  double dlina = 0;
  65.  d = arr;
  66.   minpath();
  67.  
  68.  if (arr[x][y] == inf)
  69.   dlina = -1;
  70.  else dlina = d[x][y];
  71.  
  72.  return dlina;
  73. }
  74.  
  75. void Graph::get_rasst()
  76. {
  77.   for (int i = 0; i < n; i++)
  78.   {
  79.     for (int j =0; j < n; j++)
  80.     {
  81.       cout.width(3);
  82.       cout << arr[i][j] << " ";
  83.     }
  84.     cout << endl;
  85.   }
  86. }
  87.  
  88. int main()
  89. {
  90.  int n, m;
  91.  double result, x, y, rad;
  92.  cin >> n;
  93.  
  94. coordinata *arr = new coordinata[n];
  95.  
  96.  for ( int i = 0; i < n; i++)
  97.   {
  98.     cin >> x >> y >> rad;
  99.     arr[i].new_coord(x, y, rad);
  100.   }
  101.  
  102. cin >> m;
  103.  
  104. int **zapros = new int*[m];
  105. for(int i = 0; i < m; i++)
  106.  zapros[i] = new int[2];
  107.  
  108. for (int i = 0; i < m; i++)
  109.   cin >> zapros[i][0] >> zapros[i][1];
  110.  
  111.  Graph object(arr, n);
  112.  
  113.  for (int i = 0; i < m; i++)
  114.   {
  115.     result = object.get_length_path(zapros[i][0] - 1, zapros[i][1] - 1);
  116.     cout.precision(6);
  117.     cout << fixed << result << endl;
  118.   }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement