Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const unsigned long long MOD=(long long)1e9+7;
- const long long INF=(2*(long long)1e9);
- long long cycle_start,cycle_end;
- vector<pair<int,int> > cycle;
- long long p[(long long)1e5+1]={};
- int ans=0;
- int n,m;
- bool used[101][101]={};
- pair<int,int> parent[101][101];
- int d[101][101];
- int main()
- {
- ios::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- //freopen("knight1.in","r",stdin);
- //freopen("knight1.out","w",stdout);
- cin>>n;
- cin>>m;
- int qq;
- int a[2];
- vector<vector<pair<int,int> > > g(n+1); /// 1 - v 2 - line
- while(m--)
- {
- cin>>qq;
- for(int i=0;i<qq;i++)
- {
- cin>>a[i%2];
- if(i>0)
- {
- g[a[1]].push_back(make_pair(a[0],m+1));
- g[a[0]].push_back(make_pair(a[1],m+1));
- }
- }
- }
- int s,f;
- cin>>s>>f;
- int d[n+1];
- for(int i=0;i<=n;i++)
- {
- d[i]=(int)1e9;
- }
- d[s]=0;
- set<pair<int,pair<int,int> > > q;
- q.insert(make_pair(0,make_pair(s,-1))); /// 1 - len 2 - v 3 - line;
- int v,len,line;
- while(!q.empty())
- {
- v=q.begin()->second.first;
- line=q.begin()->second.second;
- len=q.begin()->first;
- q.erase(q.begin());
- for(auto x : g[v])
- {
- if(len+(bool)(x.second!=line && line!=-1)<d[x.first])
- {
- d[x.first]=len+(bool)(x.second!=line && line!=-1);
- q.insert(make_pair(len+(bool)(x.second!=line && line!=-1),make_pair(x.first,x.second)));
- }
- }
- }
- cout<<(d[f]==(int)1e9 ? -1 : d[f]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement