Korotkodul

форд 1

Mar 27th, 2022 (edited)
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #define pii pair <int,int>
  11. #define vec vector
  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. void cvb(vector <bool> v){
  33. for (bool x: v) cout<<x<<' ';
  34. cout<<"\n";
  35. }
  36.  
  37. void cvs(vector <string> v){
  38. for (auto a: v){
  39. cout<<a<<"\n";
  40. }
  41. }
  42.  
  43.  
  44.  
  45.  
  46. struct ed{
  47. int fr, to, w;
  48. };
  49.  
  50. int inf = 2e9;
  51.  
  52. vector <int> to_comp;
  53. vector <int> clr;
  54. vector <vector <int> > Gra;//список смеж
  55.  
  56. void dfs(int v, int pr, int comp_v){
  57. clr[v] = 1;
  58. to_comp[v] = comp_v;
  59. for (int u: Gra[v]){
  60. if (u == pr) continue;
  61. if (clr[u] == 0){
  62. dfs(u, v, comp_v);
  63. }
  64. }
  65. }
  66.  
  67.  
  68. void Frd(vector <ed> G, int n, int str){
  69. int m = G.size();
  70. vector <int> d(n, inf);
  71. vector <int> pred(n); //может надо как-то для начала его заполнить?
  72. d[str] = 0;
  73. int upd_lst;
  74. for (int i = 0; i < n; ++i){
  75. upd_lst = -1;
  76. for (int j = 0; j < m; ++j){
  77. if (d[ G[j].fr ] == inf){
  78. continue;
  79. }
  80. if (d[ G[j].to ] > d[ G[j].fr ] + G[j].w){
  81. d[ G[j].to ] = d[ G[j].fr ] + G[j].w;
  82. upd_lst = G[j].to;
  83. pred[ G[j].to ] = G[j].fr;
  84. }
  85. }
  86. }
  87. if (upd_lst == -1) return;
  88.  
  89. int v = upd_lst;
  90. for (int i = 0; i < n; ++i){
  91. v = pred[v];
  92. }
  93. str = v;
  94. v = pred[v];
  95. vector <int> ans = {str};
  96. while (v != str){
  97. ans.push_back(v);
  98. v = pred[v];
  99. }
  100. ans.push_back(str);
  101. reverse(ans.begin(), ans.end());
  102. for (int i: ans) cout<<i+1<<' ';
  103. exit(0);
  104. }
  105.  
  106.  
  107. int main()
  108. {
  109. ios::sync_with_stdio(0);
  110. cin.tie(0);
  111. cout.tie(0);
  112. int n; cin>>n;
  113. int m = 0;
  114. vector <int> d(n, inf);
  115. d[0]=0;
  116. vector <ed> G;//список ребер
  117. Gra.resize(n);
  118. vector <int> pred(n);
  119. for (int i = 0; i < n ;++i){
  120. for (int j = 0; j < n;++j){
  121. int ds; cin>>ds;
  122. if (ds == 100000) continue;
  123. ed x = {i, j, ds};
  124. Gra[x.fr].push_back(x.to);
  125. m++;
  126. G.push_back(x);
  127. }
  128. }
  129. for (ed x: G){
  130. // cout<<x.fr<<' '<<x.to<<' '<<x.w<<"\n";
  131. }
  132. to_comp.assign(n, -1);
  133. clr.assign(n, 0);
  134. int cnt = -1;
  135. for (int i = 0; i < n; ++i){
  136. if (to_comp[i] == -1){
  137. cnt++;
  138. dfs(i, i, cnt);
  139. }
  140. }
  141. //cout<<"to_comp\n";
  142. for (int i = 0; i < n;++i){
  143. //cout<<i<<' '<<to_comp[i]<<"\n";
  144. }
  145. //cout<<"cnt= "<<cnt;
  146. vector <vector <ed> > comp(cnt+1);
  147. for (int i = 0; i < m; ++i){
  148. comp[ to_comp[ G[i].fr ] ].push_back( G[i] );
  149. }
  150. /*cout<<"comp\n";
  151. for (int i = 0; i < cnt+1;++i){
  152. cout<<i<<"\n";
  153. for(auto x: comp[i]){
  154. cout<<x.fr<<' '<<x.to<<' '<<x.w<<"\n";
  155. }
  156. }*/
  157. vector <int> comp_size(cnt+1,0);
  158. for (int i = 0; i < n;++i){
  159. comp_size[ to_comp[i] ]++;
  160. }
  161. for (int i = 0; i < cnt + 1; ++i){
  162. int str = comp[i][0].fr;
  163. Frd(comp[i], comp_size[i], str);
  164. }
  165. }
  166. /*
  167. 13
  168. 100000 1 100000 100 100000 100000 100000 100000 100000 100000 100000 100000 100000
  169. 100000 100000 10 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000
  170. 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000
  171. 100000 -10 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000
  172. 100000 100000 100000 100000 100000 1 100000 100000 100000 100000 100000 100000 100000
  173. 100000 100000 100000 100000 100000 100000 2 100000 -10 100000 100000 100000 100000
  174. 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000
  175. 100000 100000 100000 100000 100000 100000 3 100000 4 100000 100000 100000 100000
  176. 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000
  177. 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 1 100000 3
  178. 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 2 100000
  179. 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 -7
  180. 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 4 0 100000
  181. */
  182.  
Add Comment
Please, Sign In to add comment