Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <math.h>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <iomanip>
  7. #include <queue>
  8. #include <stack>
  9. #include <climits>
  10.  
  11. using namespace std;
  12. long n, m, a, b;
  13. vector <vector <double>> mas;
  14.  
  15.  
  16. struct ostrov {
  17.    long x, y, r;
  18. };
  19.  
  20. vector <ostrov> ostrov1;
  21.  
  22. void dk() {
  23.     vector <bool> used(n);
  24.     vector <double> l(n, INT_MAX);
  25.     l[a] = 0;
  26.  
  27.     for (long i = 0; i < n; i++)
  28.     {
  29.         long min = INT_MAX;
  30.         long s = INT_MIN;
  31.         for (long j = 0; j < n; j++)
  32.         {
  33.             if (!used[j] && l[j] < min) {
  34.                 min = l[j];
  35.                 s = j;
  36.             }
  37.         }
  38.         if (s == INT_MIN) break;
  39.        
  40.         for (long j = 0; j < n; j++){
  41.             if (!used[j] && mas[s][j] != -1) {
  42.                 if (l[s] + mas[s][j] < l[j]) {
  43.                     l[j] = l[s] + mas[s][j];
  44.                 }
  45.             }
  46.         }
  47.         used[s] = true;
  48.     }
  49.     printf("%lf ", l[b]);
  50.        
  51. }
  52.  
  53.  
  54. int main() {
  55.    // freopen("input.txt", "r", stdin);
  56.    // freopen("output.txt", "w", stdout);
  57.     cin >> n;
  58.     mas.resize(n); ostrov1.resize(n);
  59.    
  60.     for (long i = 0; i < n; i++){
  61.         mas[i].resize(n);
  62.         cin >> ostrov1[i].x >> ostrov1[i].y >> ostrov1[i].r;
  63.        
  64.     }
  65.     for (long i = 0; i < n; i++) {
  66.         for (long j = i + 1; j < n; j++){
  67.  
  68.             mas[i][j] =  mas[j][i] =sqrt(abs(ostrov1[i].x - ostrov1[j].x) * abs(ostrov1[i].x - ostrov1[j].x)
  69.                 + abs(ostrov1[i].y - ostrov1[j].y) * abs(ostrov1[i].y - ostrov1[j].y))
  70.                 - ostrov1[i].r - ostrov1[j].r;
  71.            
  72.  
  73.         }
  74.     }
  75.  
  76.     cin >> m;
  77.     for (long i = 0; i < m; i++){
  78.         cin >> a >> b;
  79.         a--; b--;
  80.         dk();
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement