Advertisement
welleyth

Метро

Aug 10th, 2020
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const unsigned long long MOD=(long long)1e9+7;
  6. const long long INF=(2*(long long)1e9);
  7. long long cycle_start,cycle_end;
  8.  
  9. vector<pair<int,int> > cycle;
  10.  
  11. long long p[(long long)1e5+1]={};
  12.  
  13. int ans=0;
  14. int n,m;
  15.  
  16. bool used[101][101]={};
  17. pair<int,int> parent[101][101];
  18. int d[101][101];
  19.  
  20. int main()
  21. {
  22.     ios::sync_with_stdio(false);
  23.     cin.tie(0);
  24.     cout.tie(0);
  25.     //freopen("knight1.in","r",stdin);
  26.     //freopen("knight1.out","w",stdout);
  27.     cin>>n;
  28.     cin>>m;
  29.     int qq;
  30.     int a[2];
  31.     vector<vector<pair<int,int> > > g(n+1); /// 1 - v 2 - line
  32.     while(m--)
  33.     {
  34.         cin>>qq;
  35.         for(int i=0;i<qq;i++)
  36.         {
  37.             cin>>a[i%2];
  38.             if(i>0)
  39.             {
  40.                 g[a[1]].push_back(make_pair(a[0],m+1));
  41.                 g[a[0]].push_back(make_pair(a[1],m+1));
  42.             }
  43.         }
  44.     }
  45.     int s,f;
  46.     cin>>s>>f;
  47.     int d[n+1];
  48.     for(int i=0;i<=n;i++)
  49.     {
  50.         d[i]=(int)1e9;
  51.     }
  52.     d[s]=0;
  53.     set<pair<int,pair<int,int> > > q;
  54.     q.insert(make_pair(0,make_pair(s,-1))); /// 1 - len 2 - v 3 - line;
  55.     int v,len,line;
  56.     while(!q.empty())
  57.     {
  58.         v=q.begin()->second.first;
  59.         line=q.begin()->second.second;
  60.         len=q.begin()->first;
  61.         q.erase(q.begin());
  62.         for(auto x : g[v])
  63.         {
  64.             if(len+(bool)(x.second!=line && line!=-1)<d[x.first])
  65.             {
  66.                 d[x.first]=len+(bool)(x.second!=line && line!=-1);
  67.                 q.insert(make_pair(len+(bool)(x.second!=line && line!=-1),make_pair(x.first,x.second)));
  68.             }
  69.         }
  70.     }
  71.     cout<<(d[f]==(int)1e9 ? -1 : d[f]);
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement