irapilguy

Untitled

Nov 28th, 2017
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 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;
  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.                 used[i] = 1;               
  23.             }
  24.             int per = graph[start][i] + lenght[start];
  25.             if (lenght[i] != 0 && graph[start][i] != -1 && lenght[i] > per) {
  26.                 lenght[i] = per;
  27.                 q.push(i);
  28.                 way.push_back(i);
  29.             }
  30.         }
  31.     }
  32. }
  33. int main() {
  34.     int start, finish;
  35.     cin >> n >> start >> finish;
  36.     for (int j = 0; j < n; j++) {
  37.         for (int i = 0; i < n; i++) {
  38.             int x;
  39.             cin >> x;
  40.             graph[j].push_back(x);
  41.         }
  42.     }
  43.     for (int i = 0; i < n; i++) lenght[i] = INF;
  44.     lenght[start - 1] = 0;
  45.     bfs(start - 1);
  46.     cout << "lenght " << lenght[finish - 1] << "\n";
  47.     int end = way.size();
  48.     cout << start << " ";
  49.     for (int i = 1; i < end; i++)   cout << way[i]+ 1 << " ";
  50.     return 0;
  51. }
Add Comment
Please, Sign In to add comment