Advertisement
Korotkodul

ЗОШ 0-1 bfs OK

Jan 12th, 2022 (edited)
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <deque>
  6. #include <algorithm>
  7. #include <string>
  8. #include <stack>
  9. #include <set>
  10. #include <map>
  11. #define pii pair <int,int>
  12. using namespace std;
  13. using ll = long long;
  14. using ld = long double;
  15. using db = double;
  16. void cv(vector <int> &v){
  17. for (auto x: v) cout<<x<<' ';
  18. cout<<"\n";
  19. }
  20.  
  21. void cvl(vector <ll> &v){
  22. for (auto x: v) cout<<x<<' ';
  23. cout<<"\n";
  24. }
  25.  
  26.  
  27. void cvv(vector <vector <int> > &v){
  28. for (auto x: v) cv(x);
  29. cout<<"\n";
  30. }
  31.  
  32. struct ed{
  33. int fr, to, w;
  34. };
  35.  
  36. vector <vector <ed>> G;
  37. int n,m;
  38.  
  39. vector <int> d;
  40.  
  41. deque <int> Q;
  42.  
  43. vector <int> pr;
  44.  
  45. const int inf = 2e9;
  46.  
  47. bool ch = 1;
  48.  
  49.  
  50. void bfs(int s){
  51. d.resize(n, inf);
  52. pr.resize(n, -100);
  53. d[s] = 0;
  54. Q.push_back(s);
  55. while (!Q.empty()){
  56. int v = Q.front();
  57. Q.pop_front();
  58. for (auto e: G[v]){
  59. if (d[v] + e.w < d[e.to]){
  60. d[e.to] = d[v] + e.w;
  61. pr[e.to] = v;
  62. if (e.w == 1){
  63. Q.push_back(e.to);
  64. }
  65. else{
  66. Q.push_front(e.to);
  67. }
  68. }
  69. }
  70. }
  71. }
  72.  
  73. void shG(){
  74. for (int i = 0; i < n;++i){
  75. cout<<i+1<<"\n";
  76. for (auto e: G[i]){
  77. cout<<e.to+1<<' '<<e.w<<"\n";
  78. }
  79. cout<<"\n";
  80. }
  81. }
  82.  
  83. void shd(){
  84. cout<<"dst\n";
  85. for (int i=0;i<n;++i){
  86. cout<<i+1<<' '<<d[i]<<"\n";
  87. }
  88. cout<<"\n";
  89. }
  90.  
  91. void shpr(){
  92. cout<<"pr\n";
  93. for (int i =0;i<n;++i){
  94. cout<<i+1<<' '<<pr[i]+1<<"\n";
  95. }
  96. cout<<"\n";
  97. }
  98.  
  99. int main()
  100. {
  101. ios::sync_with_stdio(0);
  102. cin.tie(0);
  103. cout.tie(0);
  104. cin>>n>>m;
  105. G.resize(n);
  106. vector <int> whose(n);
  107. for (int i=0;i<n;++i){
  108. cin>>whose[i];
  109. }
  110.  
  111. for (int i =0;i<m;++i){
  112. int a,b, w;
  113. cin>>a>>b;
  114. a--; b--;
  115. if (whose[a] == whose[b]){
  116. w = 0;
  117. }
  118. else w = 1;
  119. ed any = {a, b, w};
  120. G[a].push_back(any);
  121. any = {b, a, w};
  122. G[b].push_back(any);
  123. }
  124. //shG();
  125. bfs(0);
  126. if (d[n-1] == inf){
  127. cout<<"impossible\n";
  128. exit(0);
  129. }
  130. //cout<<d[n-1];
  131. //shd();
  132. //shpr();
  133. vector <int> ans = {n};
  134. int v = n-1;
  135. while (pr[v] > 0){
  136. ans.push_back(pr[v] + 1);
  137. v = pr[v];
  138. }
  139. ans.push_back(1);
  140. cout<<d[n-1]<<' '<<ans.size()<<"\n";
  141. reverse(ans.begin(), ans.end());
  142. cv(ans);
  143. //кек
  144. }
  145. /*
  146. 7 8
  147. 1 1 1 1 2 2 1
  148. 1 2
  149. 2 5
  150. 2 3
  151. 5 4
  152. 4 3
  153. 4 7
  154. 1 6
  155. 6 7
  156.  
  157. */
  158.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement