-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchefandreversing.cpp
More file actions
41 lines (38 loc) · 831 Bytes
/
chefandreversing.cpp
File metadata and controls
41 lines (38 loc) · 831 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<bits/stdc++.h>
using namespace std;
#define ll long long
vector<pair<ll,ll>> adj[100007];
bool vis[100007];
deque<ll> q;
ll n,m;
void bfs(){
vector<ll> dist(n+1,INT_MAX);
dist[1]=0;
vis[1]=1;
q.push_front(1);
while(!q.empty()){
ll prev = q.front();
q.pop_front();
for( auto it : adj[prev]){
ll x = it.first;
ll y = it.second;
if(dist[x]>dist[prev]+y){
dist[x]=dist[prev]+y;
if(y==1) q.push_back(x);
else q.push_front(x);
}
}
}
if(dist[n]==INT_MAX) dist[n]=-1;
cout<<dist[n]<<"\n";
}
int main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
ll a,b;
cin>>a>>b;
adj[a].push_back({b,0});
adj[b].push_back({a,1});
}
bfs();
}