fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <utility>
  4. #include <deque>
  5.  
  6. #define vi vector<int>
  7. #define pii pair<int,int>
  8. #define vpii vector<pii>
  9. #define dqi deque<int>
  10.  
  11. #define pb push_back
  12. #define pf push_front
  13. #define ppf pop_front
  14. #define ppb pop_back
  15. #define mp make_pair
  16. #define INFINITY (1<<20)
  17.  
  18.  
  19. using namespace std;
  20.  
  21. int main()
  22. {
  23. int n,m,from,to;
  24.  
  25. cin>>n>>m;
  26.  
  27. vector<vpii> adj(n+1);
  28. vi d(n+1,INFINITY),color(n+1,1);
  29. dqi q;
  30.  
  31. for(int i=0;i<m;i++){
  32.  
  33. cin>>from>>to;
  34.  
  35. adj[from].pb(mp(to,0));
  36. adj[to].pb(mp(from,1));
  37.  
  38. }
  39.  
  40. d[1]=0;
  41. color[1]=2;
  42. q.pf(1);
  43.  
  44. int u,v,w,size;
  45.  
  46. while(!q.empty()){
  47.  
  48. u=q.front();
  49. q.ppf();
  50. size=adj[u].size();
  51.  
  52. for(int i=0;i<size;i++){
  53.  
  54. v=adj[u][i].first;
  55. w=adj[u][i].second;
  56. if(d[v]>d[u]+w){
  57.  
  58. if(w){
  59. q.pb(v);
  60. d[v]=d[u]+1;
  61. }
  62.  
  63. else{
  64. q.pf(v);
  65. d[v]=d[u];
  66. }
  67.  
  68.  
  69.  
  70. }
  71. //color[u]=0;
  72. }
  73. }
  74.  
  75. if(d[n]==INFINITY)cout<<-1;
  76. else cout<<d[n];
  77.  
  78. cout<<endl;
  79.  
  80. return 0;
  81. }
Runtime error #stdin #stdout #stderr 0s 3464KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc