Advertisement
Guest User

Untitled

a guest
May 27th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. vector<int> G[1000008];
  4. int odw[1000008];
  5. int odl[1000008];
  6.  
  7. int wybieg[1000008];
  8. queue<int> Q;
  9. int krawedzie, wezly, a, b, w, v, wynik=INT_MAX;
  10.  
  11. void czytaj();
  12. void wypisz_graf();
  13. void wypisz_ojca();
  14. void wypisz_odl();
  15. void BFS(int s);
  16.  
  17. int main()
  18. {
  19. ios_base::sync_with_stdio(0);
  20. cin.tie(NULL);
  21.  
  22. czytaj();
  23. for(int i=1; i<=wezly; i++)
  24. {
  25. if(wybieg[i]==0)BFS(i);
  26. }
  27.  
  28.  
  29.  
  30.  
  31.  
  32. wypisz_odl();
  33. cout<<wynik;
  34. return 0;
  35. }
  36.  
  37. void czytaj()
  38. {
  39. cin>>wezly>>krawedzie;
  40. for(int i=1; i<=wezly; i++)
  41. {
  42. cin>>wybieg[i];
  43. }
  44. for(int i=1; i<=krawedzie; i++)
  45. {
  46. cin>>a>>b;
  47.  
  48. G[a].push_back(b);
  49. G[b].push_back(a);
  50. }
  51. }
  52. void BFS(int s)
  53. {
  54. odw[s]=1;
  55.  
  56. odl[s]=0;
  57.  
  58. Q.push(s);
  59.  
  60. while(!Q.empty())
  61. {
  62. w=Q.front();
  63. Q.pop();
  64.  
  65. for(int i=0; i<G[w].size(); i++)
  66. {
  67. int v=G[w][i];
  68. if(odw[v]==0)
  69. {
  70.  
  71. odw[v]=1;
  72.  
  73. if(odl[v]==0 || odl[w]+1 < odl[v])odl[v]=odl[w]+1;
  74. Q.push(v);
  75.  
  76. }
  77. }
  78. }
  79. }
  80.  
  81.  
  82.  
  83.  
  84. void wypisz_odl()
  85. {
  86.  
  87. for(int i=1; i<=wezly; i++)
  88. {
  89. if(wybieg[i]==1 && odl[i]<wynik)
  90. {
  91. wynik=odl[i];
  92. }
  93.  
  94.  
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement