Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- #define N 101
- vector <int> mas[N];
- int res[N];
- int used[N];
- int n;
- void dfs(int start, int finish) {
- if (used[start] == 1) return;
- used[start] = 1;
- for (int i = 0; i < n; i++) {
- if (mas[start][i] != -1) res[i] = mas[start][i];
- if (mas[start][i] > 0) {
- if (res[i] > mas[start][i]) res[i] = mas[start][i];
- dfs(i, finish);
- }
- }
- }
- int main() {
- int start, finish;
- cin >> n >> start >> finish;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- int x;
- cin >> x;
- mas[i].push_back(x);
- }
- }
- dfs(start - 1, finish - 1);
- int way = 0;
- for (int i = 0; i < n; i++) way += res[i];
- if (way != 0) cout << way;
- else cout << "-1";
- return 0;
- }
Add Comment
Please, Sign In to add comment