Advertisement
Korotkodul

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

Jan 19th, 2022 (edited)
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 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,m;
  32. struct ed{
  33. int fr, to, w;
  34. };
  35. vector <ed> G;
  36. const ll inf = 1e18;
  37. vector <ll> dst;
  38. vector <ll> check;
  39. void frd(){
  40. dst.resize(n, -inf);
  41. dst[0] = 0;
  42. for (int l = 0; l < n-1; ++l){
  43. // cout<<"l = "<<l<<"\n";
  44. for (int i = 0; i < m; ++i){
  45. if (dst[G[i].fr] == -inf) continue;
  46. if (dst[G[i].to] < dst[G[i].fr] + G[i].w){
  47. dst[G[i].to] = dst[G[i].fr] + G[i].w;
  48. }
  49. }
  50. }
  51. }
  52.  
  53. vector <vector <bool> > can;//попасть из i в j
  54.  
  55. const int wh = 900, gr = 678, bl = 999;
  56.  
  57.  
  58. vector <bool> here;
  59. vector <int> clr;
  60. vector <vector<int>> goG;
  61. void dfs(int v){
  62. clr[v] = gr;
  63. here[v] = 1;
  64. for (int u: goG[v]){
  65. if (clr[u] == wh){
  66. dfs(u);
  67. }
  68. }
  69. clr[v] = bl;
  70. }
  71.  
  72.  
  73. void connect(){
  74. for (int i = 0; i < n; ++i){
  75. clr.assign(n, wh);
  76. here.assign(n, 0);
  77. dfs(i);
  78. can.push_back(here);
  79. }
  80. }
  81. void cvb(vector <bool> v){
  82. for (bool x: v) cout<<x<<' ';
  83. cout<<"\n";
  84. }
  85.  
  86. void shCan(){
  87. cout<<"can\n";
  88. for (int i=0;i<n;++i){
  89. cout<<i+1<<" : ";
  90. cvb(can[i]);
  91.  
  92. }cout<<"\n";
  93. }
  94.  
  95. void chk(){
  96. if (dst[n-1] == -inf){
  97. cout<<":(\n";
  98. exit(0);
  99. }
  100. connect();
  101. check = dst;
  102. for (int l = 0; l < n-1; ++l){
  103. // cout<<"l = "<<l<<"\n";
  104. for (int i = 0; i < m; ++i){
  105. if (check[G[i].fr] == -inf) continue;
  106. if (check[G[i].to] < check[G[i].fr] + G[i].w){
  107. check[G[i].to] = check[G[i].fr] + G[i].w;
  108. }
  109. }
  110. }
  111. //cout<<"dst check\n";
  112. //cvl(dst); cvl(check);
  113. /*if (check[n-1] > dst[n-1]){
  114. cout<<":)\n";
  115. exit(0);
  116. }*/
  117. //shCan();
  118. for (int i=0;i<n;++i){
  119. if (dst[i] > -inf && can[i][n-1] == 1 && check[i] > dst[i]){
  120. cout<<":)\n";
  121. exit(0);
  122. }
  123. }
  124. }
  125.  
  126.  
  127.  
  128.  
  129.  
  130. void shG(){
  131. for (int i=0;i<m;++i){
  132. cout<<G[i].fr+1<<' '<<G[i].to+1<<' '<<G[i].w<<"\n";
  133. }
  134. }
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142. int main()
  143. {
  144. ios::sync_with_stdio(0);
  145. cin.tie(0);
  146. cout.tie(0);
  147. cin>>n>>m;
  148. G.resize(m);
  149. goG.resize(n);
  150. for (int i = 0; i < m;++i) {
  151. cin>>G[i].fr>>G[i].to>>G[i].w;
  152. G[i].fr--;
  153. G[i].to--;
  154. goG[G[i].fr].push_back(G[i].to);
  155. }
  156. //shG();
  157. frd();
  158. chk();
  159. cout<<dst[n-1]<<"\n";
  160. //cout<<dst[n-1]<<"\n";
  161. }
  162. /*
  163. 2 2
  164. 1 2 3
  165. 1 2 7
  166.  
  167. 5 6
  168. 1 5 1
  169. 1 2 -10000
  170. 3 2 2
  171. 2 4 0
  172. 4 3 -1
  173. 2 1 -10000
  174. */
  175.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement