Advertisement
GrandtherAzaMarks

Bellman Ford, Levet

Mar 21st, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. https://neerc.ifmo.ru/wiki/index.php?title=Алгоритм_Левита
  2.  
  3. #include<iostream>
  4. #include<vector>
  5. #include<queue>
  6.  
  7. #define mp(X, Y)  make_pair(X, Y)
  8.  
  9. using namespace std;
  10.  
  11. int main() {
  12.     int N = 0, M = 0;
  13.     cin >> N >> M;
  14.     N++;
  15.     vector< vector< pair<int, int> > > G(N);
  16.     vector<int> R(N, 30000);
  17.     for(int i = 0; i < M; i++) {
  18.         int from =0, to = 0, w = 0;
  19.         cin >> from >> to >> w;
  20.         G[from].push_back(mp(to, w));
  21.     }
  22.     R[1] = 0;
  23.     queue<int> q;
  24.     q.push(1);
  25.     while(!q.empty()){
  26.         int from = q.front();
  27.         q.pop();
  28.         for(int i = 0; i < G[from].size(); i++){
  29.             int to = G[from][i].first;
  30.             int w = G[from][i].second;
  31.             if (R[from] + w < R[to]) {
  32.                 q.push(to);
  33.                 R[to] = R[from] + w;
  34.             }
  35.         }
  36.     }
  37.     for (int i = 1; i < R.size(); i++){
  38.         cout << R[i] << " ";
  39.     }
  40.     system("pause");
  41.     return 0;
  42. }
  43.  
  44.  
  45. #include<iostream>
  46. #include<vector>
  47. #include<deque>
  48.  
  49. #define mp(X, Y)  make_pair(X, Y)
  50.  
  51. using namespace std;
  52.  
  53. int main() {
  54.     int N = 0, M = 0;
  55.     cin >> N >> M;
  56.     N++;
  57.     vector< vector< pair<int, int> > > G(N);
  58.     vector<int> R(N, 30000);
  59.     for(int i = 0; i < M; i++) {
  60.         int from =0, to = 0, w = 0;
  61.         cin >> from >> to >> w;
  62.         G[from].push_back(mp(to, w));
  63.     }
  64.     R[1] = 0;
  65.     deque<int> q;
  66.     q.push_back(1);
  67.     while(!q.empty()){
  68.         int from = q.front();
  69.         q.pop_front();
  70.         for(int i = 0; i < G[from].size(); i++){
  71.             int to = G[from][i].first;
  72.             int w = G[from][i].second;
  73.             if (R[from] + w < R[to]) {
  74.                 if(R[to] == 30000)
  75.                     q.push_back(to);
  76.                 else
  77.                     q.push_front(to);
  78.                 R[to] = R[from] + w;
  79.             }
  80.         }
  81.     }
  82.     for (int i = 1; i < R.size(); i++){
  83.         cout << R[i] << " ";
  84.     }
  85.     system("pause");
  86.     return 0;
  87. }
  88.  
  89.  
  90. #include<iostream>
  91. #include<vector>
  92. #include<deque>
  93. #include<queue>
  94. #define mp(X, Y)  make_pair(X, Y)
  95.  
  96. using namespace std;
  97.  
  98. int main() {
  99.     int N = 0, M = 0;
  100.     cin >> N >> M;
  101.     N++;
  102.     vector< vector< pair<int, int> > > G(N);
  103.     vector<int> R(N, 30000); // расстояния
  104.     for(int i = 0; i < M; i++) {
  105.         int from =0, to = 0, w = 0;
  106.         cin >> from >> to >> w;
  107.         G[from].push_back(mp(to, w));
  108.     }
  109.     // Инициализация, строим дерево от вершины 1
  110.     R[1] = 0;
  111.     queue<int> q1, q2; // q1 - основная, q2 - срочной
  112.     q1.push(1);
  113.     while(!q1.empty() || !q2.empty()){
  114.         int from = 0;
  115.         if (!q2.empty()){
  116.             from = q2.front();
  117.             q2.pop();
  118.         } else {
  119.             from = q1.front();
  120.             q1.pop();
  121.         }
  122.         for(int i = 0; i < G[from].size(); i++){ // просмотр инцидентных вершин
  123.             int to = G[from][i].first; // инцидентная вершина
  124.             int w = G[from][i].second; // вес дуги
  125.             if (R[from] + w < R[to]) { // проверяем, можем ли улучьшить путь
  126.                 if(R[to] == 30000){
  127.                     q1.push(to);
  128.                 } else {
  129.                     q2.push(to);
  130.                 }
  131.                 R[to] = R[from] + w; // улучшаем
  132.             }
  133.         }
  134.     }
  135.     for (int i = 1; i < R.size(); i++){
  136.         cout << R[i] << " ";
  137.     }
  138.     system("pause");
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement