Advertisement
Korotkodul

Форд Беллман ОК

Jan 19th, 2022
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 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. 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. int main()
  53. {
  54. ios::sync_with_stdio(0);
  55. cin.tie(0);
  56. cout.tie(0);
  57. cin>>n>>m;
  58. G.resize(m);
  59. for (int i = 0; i < m;++i) {
  60. cin>>G[i].fr>>G[i].to>>G[i].w;
  61. G[i].fr--;
  62. G[i].to--;
  63. }
  64. frd();
  65. for (int i = 0; i < n;++i) {
  66. if (dst[i] == inf) {
  67. cout<< 30000<<' ';
  68. continue;
  69. }
  70. cout<<dst[i]<<' ';
  71. }
  72. cout<<"\n";
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement