Advertisement
Korotkodul

форд белман

Mar 27th, 2022 (edited)
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 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. int main()
  69. {
  70. ios::sync_with_stdio(0);
  71. cin.tie(0);
  72. cout.tie(0);
  73. int n; cin>>n;
  74. int m = 0;
  75. vector <int> d(n, inf);
  76. d[0]=0;
  77. vector <ed> G;//список ребер
  78. Gra.resize(n);
  79. vector <int> pred(n);
  80. for (int i = 0; i < n ;++i){
  81. for (int j = 0; j < n;++j){
  82. int ds; cin>>ds;
  83. if (ds == 100000) continue;
  84. ed x = {i, j, ds};
  85. Gra[x.fr].push_back(x.to);
  86. m++;
  87. G.push_back(x);
  88. }
  89. }
  90. for (ed x: G){
  91. // cout<<x.fr<<' '<<x.to<<' '<<x.w<<"\n";
  92. }
  93. to_comp.assign(n, -1);
  94. clr.assign(n, 0);
  95. int cnt = -1;
  96. for (int i = 0; i < n; ++i){
  97. if (to_comp[i] == -1){
  98. cnt++;
  99. dfs(i, i, cnt);
  100. }
  101. }
  102. //cout<<"to_comp\n";
  103. for (int i = 0; i < n;++i){
  104. //cout<<i<<' '<<to_comp[i]<<"\n";
  105. }
  106. //cout<<"cnt= "<<cnt;
  107. vector <vector <ed> > comp(cnt+1);
  108. for (int i = 0; i < m; ++i){
  109. comp[ to_comp[ G[i].fr ] ].push_back( G[i] );
  110. }
  111. /*cout<<"comp\n";
  112. for (int i = 0; i < cnt+1;++i){
  113. cout<<i<<"\n";
  114. for(auto x: comp[i]){
  115. cout<<x.fr<<' '<<x.to<<' '<<x.w<<"\n";
  116. }
  117. }*/
  118. }
  119. /*
  120. 13
  121. 100000 1 100000 100 100000 100000 100000 100000 100000 100000 100000 100000 100000
  122. 100000 100000 10 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000
  123. 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000
  124. 100000 -10 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000
  125. 100000 100000 100000 100000 100000 1 100000 100000 100000 100000 100000 100000 100000
  126. 100000 100000 100000 100000 100000 100000 2 100000 -10 100000 100000 100000 100000
  127. 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000
  128. 100000 100000 100000 100000 100000 100000 3 100000 4 100000 100000 100000 100000
  129. 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000
  130. 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 1 100000 3
  131. 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 2 100000
  132. 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 -7
  133. 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 4 0 100000
  134. */
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement