Advertisement
Josif_tepe

Untitled

Sep 12th, 2023
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. #include <queue>
  2. #include <iostream>
  3. #include <vector>
  4. #include <cstring>
  5. #include <iostream>
  6. #include <bits/stdc++.h>
  7. using namespace std;
  8.  
  9.  
  10.  
  11.  
  12.  
  13. struct node{
  14.         int indx;
  15.         double dis;
  16.         node()
  17.         {
  18.  
  19.  
  20.  
  21.  
  22.        }
  23.         node(int indx1, double dis1)
  24.         {
  25.             indx=indx1;
  26.             dis=dis1;
  27.         }
  28.         bool operator < (const node &tmp) const{
  29.             return dis>tmp.dis;
  30.         }
  31.     };
  32. int main()
  33. {
  34.     int n;
  35.     cin>>n;
  36.     vector < pair<int,int> > v;
  37.     int x,y;
  38.     for(int i=0;i<n;i++)
  39.     {
  40.         cin>>x>>y;
  41.         v.push_back(make_pair(x,y));
  42.     }
  43.     vector < pair<int, double> > g[n];
  44.  
  45.  
  46.  
  47.  
  48.    vector<bool> visited(n,false);
  49.     vector<double> dis(n,2e9);
  50.     for(int i=0;i<n;i++)
  51.     {
  52.         for(int j=i;j<n;j++)
  53.         {
  54.             double d=sqrt(((v[j].first-v[i].first) * (v[j].first-v[i].first))+((v[j].second-v[i].second)*(v[j].second-v[i].second)));
  55.             if(d<=10)
  56.                 g[i].push_back(make_pair(j,d));
  57.         }
  58.     }
  59.     dis[0]=0;
  60.     priority_queue<node> pq;
  61.     pq.push(node(0,0));
  62.  
  63.  
  64.  
  65.  
  66.    while(!pq.empty())
  67.     {
  68.         node c=pq.top();
  69.  
  70.         pq.pop();
  71.  
  72.  
  73.        if(visited[c.indx])
  74.         {
  75.             continue;
  76.         }
  77.         visited[c.indx]=true;
  78.         for(int i=0;i<g[c.indx].size();i++)
  79.         {
  80.             int s=g[c.indx][i].first;
  81.             double t=g[c.indx][i].second;
  82.  
  83.  
  84.  
  85.  
  86.            if(!visited[s] && c.dis+t<dis[s])
  87.             {
  88.                 pq.push(node(s,c.dis+t));
  89.                 dis[s]=c.dis+t;
  90.             }
  91.         }
  92.     }
  93.     cout<<dis[n-1]<<endl;
  94.     return 0;
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement