Advertisement
Korotkodul

ЗОШ Dijkstra OK

Jan 12th, 2022
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #define pii pair <int,int>
  11. using namespace std;
  12. using ll = long long;
  13. using ld = long double;
  14. using db = double;
  15. void cv(vector <int> &v){
  16. for (auto x: v) cout<<x<<' ';
  17. cout<<"\n";
  18. }
  19.  
  20. void cvl(vector <ll> &v){
  21. for (auto x: v) cout<<x<<' ';
  22. cout<<"\n";
  23. }
  24.  
  25.  
  26. void cvv(vector <vector <int> > &v){
  27. for (auto x: v) cv(x);
  28. cout<<"\n";
  29. }
  30.  
  31. struct ed{
  32. int fr, to;
  33. ld w;
  34. };
  35.  
  36. vector < vector <ed> > G;
  37.  
  38.  
  39.  
  40. set <pii> Q;
  41.  
  42. vector <int> d;
  43.  
  44.  
  45. const int inf = 2e9;
  46. int n,s,f;
  47.  
  48. void Dij(int s){
  49.  
  50. d.assign(n, inf);
  51.  
  52. //cv(d);
  53.  
  54. Q.insert({0, s});
  55.  
  56. d[s] = 0;
  57.  
  58. while (!Q.empty()){
  59. int v = Q.begin()->second;
  60.  
  61. Q.erase(Q.begin());
  62. for (ed e: G[v]){
  63. int nv = e.to;
  64. if (d[nv] > d[v] + e.w){
  65. Q.insert({d[v] + e.w, nv});
  66. d[nv] = d[v] + e.w;
  67. }
  68. }
  69. }
  70. }
  71.  
  72. int main()
  73. {
  74. ios::sync_with_stdio(0);
  75. cin.tie(0);
  76. cout.tie(0);
  77.  
  78. cin>>n>>s>>f;
  79. s--;
  80. f--;
  81. G.resize(n);
  82. for (int i = 0; i < n; ++i){
  83. for (int j = 0 ;j < n;++j){
  84. int di;
  85. cin>>di;
  86. if (i == j) continue;
  87. if (di == -1) continue;
  88. G[i].push_back({i, j, di});
  89. }
  90. }
  91. Dij(s);
  92. if (d[f] == inf){
  93. cout<<-1;
  94. exit(0);
  95. }
  96. cout<<d[f]<<"\n";
  97. }
  98. /*
  99. 3 1 2
  100. 0 -1 2
  101. 3 0 -1
  102. -1 4 0
  103.  
  104.  
  105. */
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement