Advertisement
Josif_tepe

Untitled

Dec 12th, 2023
565
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5. const int maxn = 1e5 + 10;
  6. const double INF = 2e9;
  7. vector<pair<int, double> > graph[maxn];
  8. int n, m;
  9. struct node {
  10.     int idx;
  11.     double cost;
  12.     node () {}
  13.     node(int _idx, double _cost) {
  14.         idx = _idx;
  15.         cost = _cost;
  16.     }
  17.     bool operator < (const node & tmp) const {
  18.         return cost > tmp.cost;
  19.     }
  20. };
  21. void dijkstra(int S, int E) {
  22.     vector<double> dist(n, INF);
  23.     vector<bool> visited(n, false);
  24.    
  25.     dist[S] = 0;
  26.     priority_queue<node> pq;
  27.     pq.push(node(S, 0));
  28.    
  29.     while(!pq.empty()) {
  30.         node c = pq.top();
  31.         pq.pop();
  32.        
  33.         if(visited[c.idx]) continue;
  34.         visited[c.idx] = true;
  35.        
  36.         for(pair<int, double> tmp : graph[c.idx]) {
  37.             int neighbour = tmp.first;
  38.             double weight = tmp.second;
  39.            
  40.             if(!visited[neighbour] and c.cost + weight < dist[neighbour]) {
  41.                 pq.push(node(neighbour, c.cost + weight));
  42.                 dist[neighbour] = c.cost + weight;
  43.             }
  44.         }
  45.     }
  46.     printf("%.6lf\n", dist[E]);
  47. }
  48. int main() {
  49.     int t;
  50.     cin >> t;
  51.     vector<pair<double, double> > v;
  52.     for(int i = 0; i < t; i++) {
  53.         double x, y;
  54.         cin >> x >> y;
  55.         v.push_back(make_pair(x, y));
  56.     }
  57.     for(int i = 0; i < t; i++) {
  58.         for(int j = 0; j < t; j++) {
  59.             double d = sqrt((v[i].first - v[j].first) * (v[i].first - v[j].first) + (v[i].second - v[j].second) * (v[i].second - v[j].second));
  60.             if(d <= 10) {
  61.                 graph[i].push_back(make_pair(j, d));
  62.                 graph[j].push_back(make_pair(i, d));
  63.                
  64.             }
  65.         }
  66.     }
  67.    
  68.     n = t;
  69.     dijkstra(0, n - 1);
  70.    
  71.     return 0;
  72. }
  73. /*
  74.  5 5
  75.  1 2 2
  76.  2 3 4
  77.  1 3 2
  78.  3 4 5
  79.  4 5 6
  80.  **/
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement