irapilguy

Untitled

Nov 25th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. #define N 101
  5. vector <int> mas[N];
  6. int res[N];
  7. int used[N];
  8. int n;
  9. void dfs(int start, int finish) {
  10.     if (used[start] == 1) return;
  11.     used[start] = 1;
  12.     for (int i = 0; i < n; i++) {
  13.         if (mas[start][i] != -1) res[i] = mas[start][i];
  14.         if (mas[start][i] > 0) {
  15.             if (res[i] > mas[start][i]) res[i] = mas[start][i];
  16.             dfs(i, finish);
  17.         }
  18.     }
  19. }
  20. int main() {
  21.     int start, finish;
  22.     cin >> n >> start >> finish;
  23.     for (int i = 0; i < n; i++) {
  24.         for (int j = 0; j < n; j++) {
  25.             int x;
  26.             cin >> x;
  27.             mas[i].push_back(x);
  28.         }
  29.     }
  30.     dfs(start - 1, finish - 1);
  31.     int way = 0;
  32.     for (int i = 0; i < n; i++) way += res[i];
  33.     if (way != 0) cout << way;
  34.     else cout << "-1";
  35.     return 0;
  36. }
Add Comment
Please, Sign In to add comment