Advertisement
Korotkodul

ЗОШ 1-k bfs OK

Jan 12th, 2022 (edited)
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 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. int n,m;
  32. const ld inf = 2e9;
  33. const int mx_way = 5e4 + 10;
  34.  
  35. vector <queue <int> > waves;
  36.  
  37. vector <ld> d(100);
  38.  
  39.  
  40.  
  41. struct ed{
  42. int fr, to;
  43. ld w;
  44. };
  45.  
  46. vector <vector <ed>> G;
  47.  
  48. //map
  49.  
  50.  
  51.  
  52. void readG(){
  53. cin>>n>>m;
  54. G.resize(n);
  55. //cout<<"n m = "<<n<<' '<<m<<"\n";
  56. for (int i=0;i<m;++i){
  57. int a,b;
  58. ld w;
  59. cin>>a>>b>>w;
  60. a--; b--;
  61. w /= 1000.0;
  62. ed any = {a, b, w};
  63. G[a].push_back(any);
  64. }
  65. //cout<<"done\n";
  66. }
  67.  
  68.  
  69.  
  70. void shG(){
  71. cout<<"G\n";
  72. for (int i=0;i<n;++i){
  73. cout<<i+1<<"\n";
  74. for (auto x: G[i]){
  75. cout<<x.to+1<<' '<<fixed<<x.w<<"\n";
  76. }
  77. cout<<"\n";
  78. }
  79. }
  80.  
  81.  
  82. void cvld(vector <ld> v){
  83. for (int i=0;i<v.size();++i)cout<<v[i]<<' ';
  84. cout<<"\n\n";
  85. }
  86.  
  87.  
  88.  
  89. void fndWaves(int s){
  90.  
  91. d.assign(n, inf);
  92. waves.resize(mx_way);
  93. d[s] = 0;
  94. waves[0].push(s);
  95. for (int l = 0; l < mx_way; ++l){
  96. //cout<<"l = "<<l<<"\n";
  97. while (!waves[l].empty()){
  98. int v = waves[l].front();
  99. waves[l].pop();
  100. //cout<<"l = "<<l<<"\n";
  101. //cout<<"v = "<<v+1<<"\n";
  102. for (ed e: G[v]){
  103. int nv = e.to;
  104. ld nl = d[v] + e.w;
  105. //cout<<"e = "<<e.fr+1<<' '<<e.to+1<<' '<<fixed<<e.w<<"\n";
  106. if (d[nv] > nl){
  107. //if (d[e.to] != inf){
  108. // waves[l].erase(e.to);
  109. //}
  110. d[nv] = nl;
  111. //cout<<"l e.w = "<<l<<' '<<e.w<<"\n";
  112. //cout<<"d[e.to] = "<<d[e.to]<<"\n";
  113. waves[(int)floor(nl)].push(e.to);
  114. }
  115. }
  116. }
  117.  
  118. }
  119. }
  120.  
  121.  
  122. int main()
  123. {
  124. ios::sync_with_stdio(0);
  125. cin.tie(0);
  126. cout.tie(0);
  127. cout.precision(5);
  128. readG();
  129. /*int fr; cin>>fr;
  130. fndWaves(fr);
  131. //shG();
  132. cout<<"dst\n";
  133. cvld(d);*/
  134. int k=1;
  135. cin>>k;
  136. for (int i=0;i<k;++i){
  137. int a,b; cin>>a>>b;
  138. a--; b--;
  139. fndWaves(a);
  140. //cout<<"d\n";
  141. //cvld(d);
  142. //cvld(d);
  143. if (d[b] == inf){
  144. cout<<-1<<"\n";
  145. continue;
  146. }
  147. ll ans = (ll)roundl(d[b] * 1000.0);
  148. //cout<<"ans = "<<ans<<"\n";
  149. //cout<<ans<<"\n";
  150. //cout<<d[b]<<' '<<ans<<"\n";
  151. cout<<ans<<"\n";
  152. }
  153.  
  154. }
  155. /*
  156. 3 3
  157. 1 2 1001
  158. 2 3 1257
  159. 3 1 1782
  160. 10
  161.  
  162.  
  163. 5 5
  164. 1 2 2000
  165. 1 3 1000
  166. 1 4 1200
  167. 2 3 1500
  168. 3 4 1500
  169.  
  170.  
  171.  
  172. 4
  173. 1 5
  174. 2 4
  175. 3 3
  176. 1 2
  177.  
  178. */
  179.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement