Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define int long long
  4. #define F first
  5. #define S second
  6. #define pb push_back
  7.  
  8. using namespace std;
  9.  
  10. int32_t main ()
  11. {
  12. ios_base::sync_with_stdio(0);
  13. cin.tie(0);
  14. cout.tie(0);
  15.  
  16. freopen("input.txt", "r", stdin);
  17. freopen("output.txt", "w", stdout);
  18.  
  19. int n, s, f;
  20.  
  21. cin >> n >> s >> f;
  22.  
  23. s--;
  24. f--;
  25.  
  26. vector < pair < int, int >> g[n];
  27.  
  28. for (int i = 0; i < n; i++)
  29. {
  30. for (int j = 0; j < n; j++)
  31. {
  32. int x;
  33. cin >> x;
  34. if (x != -1) g[i].pb({j, x});
  35. }
  36. }
  37.  
  38. int ans[n];
  39.  
  40. for (int i = 0; i < n; i++)
  41. {
  42. ans[i] = 1e9;
  43. }
  44.  
  45. priority_queue < pair < int, int>> q;
  46.  
  47. q.push({0,s});
  48.  
  49. while (!q.empty())
  50. {
  51. int v = q.top().S, r = q.top().F;
  52. r = -r;
  53. q.pop();
  54.  
  55. if (ans[v] < r) continue;
  56.  
  57. ans[v]=r;
  58.  
  59. for (auto to:g[v])
  60. {
  61. if (ans[to.F] > r + to.S)
  62. {
  63. ans[to.F] = r + to.S;
  64. q.push({-(r + to.S), to.F});
  65. }
  66. }
  67. }
  68.  
  69. if (ans[f] == 1e9) cout << -1;
  70. else cout << ans[f];
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement