Advertisement
Mirbek

Форд-Белман

Jan 9th, 2022
796
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. /***
  2. Графы (Флойд, Дейкстра, Форд Белман)
  3. ***/
  4. #include <bits/stdc++.h>
  5.  
  6. using namespace std;
  7.  
  8. const int N = 5005;
  9. const int inf = 2e9;
  10.  
  11. int n, m;
  12. int dist[N], a[N], b[N], len[N];
  13.  
  14. int main(){
  15.  
  16.     cin >> n >> m;
  17.  
  18.     for (int i = 1; i <= m; i++) {
  19.         cin >> a[i] >> b[i] >> len[i];
  20.     }
  21.  
  22.     for (int i = 0; i < N; i++) {
  23.         dist[i] = inf;
  24.     }
  25.  
  26.     int start;
  27.     cin >> start;
  28.     dist[start] = 0;
  29.  
  30.     for (int step = 0; step < n; step++) {
  31.         for (int i = 1; i <= m; i++) {
  32.             if (dist[a[i]] + len[i] < dist[b[i]]) {
  33.                 dist[b[i]] = dist[a[i]] + len[i];
  34.             }
  35.         }
  36.     }
  37.  
  38.     int finish;
  39.     cin >> finish;
  40.  
  41.     if (dist[finish] == inf) {
  42.         cout << "No path\n";
  43.     } else {
  44.         cout << dist[finish] << endl;
  45.     }
  46. }
  47. /**
  48. 6 8
  49. 1 2 2
  50. 1 3 3
  51. 3 5 1
  52. 2 5 2
  53. 2 4 4
  54. 5 4 1
  55. 5 6 5
  56. 4 6 2
  57. 1 6
  58. ***/
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement