Norvager

http://lerna.pro/contests/202/4

Feb 9th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #define Inf  1000*1000*1000
  3. #include <vector>
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. vector <vector <int>> line;
  8. vector <int> answer;
  9.  
  10.  
  11. void dfs(int pos)
  12. {
  13.  
  14.     for (int i = 1; i < line[pos].size(); i++)
  15.     {
  16.         if (answer[i] > answer[pos] + line[pos][i] && line[pos][i] != -1)
  17.         {
  18.             answer[i] = answer[pos] + line[pos][i];
  19.             dfs(i);
  20.         }
  21.     }
  22.  
  23.     return;
  24. }
  25.  
  26. int main()
  27. {
  28. #ifndef ONLINE_JUDGE
  29.     freopen("input.txt", "r", stdin);
  30. #endif // !ONLINE_JUDGE
  31.  
  32.     int N, M;
  33.     scanf("%d %d", &N, &M);
  34.     line.resize(N + 1, vector<int>(N + 1, 0));
  35.     answer.resize(N + 1, Inf);
  36.     int a;
  37.  
  38.     for (int i = 1; i <= N; i++)
  39.     {
  40.         for (int j = 1; j <= N; j++)
  41.         {
  42.             scanf("%d", &a);
  43.             line[i][j] = a;
  44.         }
  45.     }
  46.     answer[M] = 0;
  47.     dfs(M);
  48.  
  49.     for (int i = 1; i <= N; i++)
  50.     {
  51.         if(answer[i] != Inf)
  52.             printf("%d ", answer[i]);
  53.         else printf("-1 ");
  54.     }
  55.     return 0;
  56. }
Add Comment
Please, Sign In to add comment