Advertisement
Josif_tepe

Untitled

Feb 16th, 2025
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <cmath>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. struct node {
  9.     int idx;
  10.     double shortest_path;
  11.    
  12.     node () {}
  13.     node(int _idx, double _shortest_path) {
  14.         idx = _idx;
  15.         shortest_path = _shortest_path;
  16.     }
  17.    
  18.     bool operator < (const node & tmp) const {
  19.         return shortest_path > tmp.shortest_path;
  20.     }
  21. };
  22. const int maxn = 505;
  23. int main(){
  24.     int n;
  25.     cin >> n;
  26.    
  27.     int a[n][2];
  28.     for(int i = 0; i < n; i++) {
  29.         cin >> a[i][0] >> a[i][1];
  30.     }
  31.    
  32.     vector<pair<int, double>> graph[maxn];
  33.     for(int i = 0; i < n; i++) {
  34.         for(int j = i + 1; j < n; j++) {
  35.             double distance = sqrt((a[i][0] - a[j][0]) * (a[i][0] - a[j][0]) + (a[i][1] - a[j][1]) * (a[i][1] - a[j][1]));
  36.            
  37.             if(distance <= 10.0) {
  38.                 graph[i].push_back(make_pair(j, distance));
  39.                 graph[j].push_back(make_pair(i, distance));
  40.             }
  41.         }
  42.     }
  43.    
  44.     priority_queue<node> pq;
  45.     pq.push(node(0, 0.0));
  46.    
  47.     vector<bool> visited(n, false);
  48.     vector<double> dist(n, 2e9);
  49.     dist[0] = 0.0;
  50.    
  51.     while(!pq.empty()) {
  52.         node c = pq.top();
  53.         pq.pop();
  54.        
  55.         if(visited[c.idx]) {
  56.             continue;
  57.         }
  58.         visited[c.idx] = true;
  59.        
  60.         for(int i = 0; i < (int) graph[c.idx].size(); i++) {
  61.             int neighbour = graph[c.idx][i].first;
  62.             double weight = graph[c.idx][i].second;
  63.             if(!visited[neighbour] and c.shortest_path + weight < dist[neighbour]) {
  64.                 dist[neighbour] = c.shortest_path + weight;
  65.                 pq.push(node(neighbour, dist[neighbour]));
  66.             }
  67.         }
  68.     }
  69.     cout << dist[n - 1] << endl;
  70.     return 0;
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement