Advertisement
Korotkodul

форд бэллман цикл отр-й длины

Jan 19th, 2022 (edited)
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 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. int n=0,m=0;
  32. struct ed{
  33. int fr, to, w;
  34. };
  35. vector <ed> G;
  36. const ll inf = 1e18;
  37. vector <ll> dst, check;
  38. void frd(){
  39. dst.resize(n, inf);
  40. dst[0] = 0;
  41. for (int l = 0; l < n; ++l){
  42. // cout<<"l = "<<l<<"\n";
  43. for (int i = 0; i < m; ++i){
  44. if (dst[G[i].fr] == inf) continue;
  45. if (dst[G[i].to] > dst[G[i].fr] + G[i].w){
  46. dst[G[i].to] = dst[G[i].fr] + G[i].w;
  47. }
  48. }
  49. }
  50. }
  51.  
  52. vector <int> clr;
  53. vector <bool> btr; //эту вершинку пересчитали
  54. vector <vector <int> > goG;
  55. vector <int> tpt;
  56. vector <int> ans;
  57.  
  58.  
  59. void dfs1(int v){
  60. clr[v] = 1;
  61. for (int u: goG[v]){
  62. if (clr[u] == 0){
  63. dfs1(u);
  64. }
  65. }
  66. clr[v] = 2;
  67. tpt.push_back(v);
  68. }
  69.  
  70. void topsort(){
  71. clr.assign(n, 0);
  72. for (int i = 0; i < n; ++i){
  73. if (clr[i] == 0) dfs1(i);
  74. }
  75. }
  76.  
  77.  
  78.  
  79. void dfs2(int v){
  80. clr[v] = 1;
  81. for (int u: goG[v]){
  82. if (clr[u] == 0 && btr[u] == 1){
  83. dfs2(u);
  84. }
  85. }
  86. clr[v] = 2;
  87. ans.push_back(v+1);
  88. }
  89.  
  90.  
  91.  
  92.  
  93. void chk(){
  94. check = dst;
  95. for (int rep = 0; rep < 2;++rep){
  96. for (int l = 0; l < n; ++l){
  97. for (int i = 0; i < m;++i){
  98. if (check[G[i].fr] == inf) continue;
  99. if (check[G[i].to] > check[G[i].fr] + G[i].w){
  100. check[G[i].to] = check[G[i].fr] + G[i].w;
  101. }
  102. }
  103. }
  104. }
  105. topsort();
  106. clr.assign(n, 0);
  107. btr.assign(n, 0);
  108. for (int i=0;i<n;++i){
  109. if (check[i] < dst[i]){
  110. btr[i] = 1;
  111. }
  112. }
  113. for (int i = 0; i < n;++i){
  114. if (btr[i]){
  115. dfs2(i);
  116. cout<<"YES\n";
  117. cv(ans);
  118. cout<<ans[0]<<"\n";
  119. exit(0);
  120. }
  121. }
  122. }
  123.  
  124.  
  125.  
  126. int main()
  127. {
  128. ios::sync_with_stdio(0);
  129. cin.tie(0);
  130. cout.tie(0);
  131. cin>>n;
  132. //G.resize(m);
  133. goG.resize(n);
  134. for (int i = 0; i < n;++i){
  135. for (int j = 0; j < n;++j){
  136. int d; cin>>d; if (d == 100000) continue;
  137. m++;
  138. goG[i].push_back(j);
  139. ed any = {i, j, d};
  140. G.push_back(any);
  141. }
  142. }
  143. frd();
  144. chk();
  145. cout<<"NO\n";
  146. }
  147. /*
  148. 3
  149. 100000 100000 -51
  150. 100 100000 100000
  151. 100000 -50 100000
  152. */
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement