irapilguy

Untitled

Nov 28th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include<queue>
  4. using namespace std;
  5. #define N 101
  6. #define INF 1000000
  7. vector <int> graph[N];
  8. vector <int >way[N];
  9. int lenght[N];
  10. int used[N];
  11. int n;
  12. void bfs(int start) {
  13.     queue<int>q;
  14.     q.push(start);
  15.     used[start] = 1;
  16.     while (q.size() != 0) {
  17.         start = q.front();
  18.         q.pop();
  19.         for (int i = 0; i < n; i++) {
  20.             if ( used[i] == 0 && graph[start][i] != -1) {
  21.                 q.push(i);
  22.                 way[i ].push_back(i);
  23.                 used[i] = 1;
  24.                
  25.             }
  26.             int per = graph[start][i] + lenght[start];
  27.             if (lenght[i] != 0 && graph[start][i] != -1 && lenght[i] > per) {
  28.                 lenght[i] = per;
  29.                 q.push(i);
  30.                 way[i].push_back(i);
  31.             }
  32.         }
  33.     }
  34. }
  35. int main() {
  36.     int start, finish;
  37.     cin >> n >> start >> finish;
  38.     for (int j = 0; j < n; j++) {
  39.         for (int i = 0; i < n; i++) {
  40.             int x;
  41.             cin >> x;
  42.             graph[j].push_back(x);
  43.         }
  44.     }
  45.     for (int i = 0; i < n; i++) lenght[i] = INF;
  46.     lenght[start - 1] = 0;
  47.     bfs(start - 1);
  48.     cout << "lenght " << lenght[finish - 1] << "\n";
  49.     cout << start << "  ";
  50.     int end = way[].size();
  51.     for (int i = 0; i <= end; i++)  cout << way[i].front() + 1 << " ";
  52.     return 0;
  53. }
Add Comment
Please, Sign In to add comment