Advertisement
Korotkodul

Problem B. Лабиринт знаний WA 21

Jan 19th, 2022 (edited)
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 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. void chk(){
  54. if (dst[n-1] == -inf){
  55. cout<<":(\n";
  56. exit(0);
  57. }
  58. check = dst;
  59. for (int l = 0; l < n-1; ++l){
  60. // cout<<"l = "<<l<<"\n";
  61. for (int i = 0; i < m; ++i){
  62. if (check[G[i].fr] == -inf) continue;
  63. if (check[G[i].to] < check[G[i].fr] + G[i].w){
  64. check[G[i].to] = check[G[i].fr] + G[i].w;
  65. }
  66. }
  67. }
  68. if (check[n-1] > dst[n-1]){
  69. cout<<":)\n";
  70. exit(0);
  71. }
  72. }
  73.  
  74. void shG(){
  75. for (int i=0;i<m;++i){
  76. cout<<G[i].fr+1<<' '<<G[i].to+1<<' '<<G[i].w<<"\n";
  77. }
  78. }
  79.  
  80. int main()
  81. {//отдебажь на тесте, который у тебя есть!!!
  82. ios::sync_with_stdio(0);
  83. cin.tie(0);
  84. cout.tie(0);
  85. cin>>n>>m;
  86. G.resize(m);
  87. for (int i = 0; i < m;++i) {
  88. cin>>G[i].fr>>G[i].to>>G[i].w;
  89. G[i].fr--;
  90. G[i].to--;
  91. }
  92. //shG();
  93. frd();
  94. chk();
  95. cout<<dst[n-1]<<"\n";
  96. //cout<<dst[n-1]<<"\n";
  97. }
  98. /*
  99. 2 2
  100. 1 2 3
  101. 1 2 7
  102. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement