Advertisement
Wolfed_7

Untitled

Jun 13th, 2022
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <queue>
  5. #include <iomanip>
  6.  
  7. using namespace std;
  8. void Dijkstra_vu(vector<vector<pair<float, int>>> graph, int v_start, int u_end);
  9.  
  10. // Ввод графа из файла.
  11. void test(string filename)
  12. {
  13. ifstream fin(filename);
  14.  
  15. vector<vector<pair<float, int>>> graph;
  16. int N = 0; // Количество аэропортов.
  17. float D = 0.0f; // Максимальное расстояние без посадки.
  18. int A = 0; // Где я есть.
  19. int B = 0; // Куда мне надо.
  20.  
  21. fin >> N;
  22. fin >> D;
  23. fin >> A;
  24. fin >> B;
  25. graph.resize(N);
  26.  
  27. int grad1 = 0, grad2 = 0;
  28. float rad = 3.1415927f / 180.0f;
  29. vector<pair<float, float>> coordinates;
  30. for (int i = 0; i < N; i++)
  31. {
  32. fin >> grad1; fin >> grad2;
  33. coordinates.push_back({ grad1 * rad, grad2 * rad });
  34. }
  35.  
  36. for (int i = 0; i < N; i++)
  37. {
  38. for (int j = i + 1; j < N; j++)
  39. {
  40. // Расстояние между i и j портами.
  41. float dbtw = 2.0f * asin(sqrt(pow((sin((coordinates[i].first - coordinates[j].first) / 2.0f)), 2) + cos(coordinates[i].first) * cos(coordinates[j].first) * pow((sin((coordinates[i].second - coordinates[j].second) / 2.0f)), 2)));
  42.  
  43. if (dbtw <= D || dbtw < 1e-7f)
  44. {
  45. graph[i].push_back({ dbtw, j });
  46. graph[j].push_back({ dbtw, i });
  47. }
  48. }
  49. }
  50.  
  51. Dijkstra_vu(graph, A - 1, B - 1);
  52. }
  53.  
  54. // Поиск кратчайшего пути между двумя вершинами.
  55. void Dijkstra_vu(vector<vector<pair<float, int>>> graph, int A, int B)
  56. {
  57. vector<float> d(graph.size(), FLT_MAX); // Кратчайшие расстояния до вершины.
  58.  
  59. priority_queue<pair<float, int>, vector<pair<float, int>>, greater<pair<float, int>>> Q;
  60. vector<bool> isUsed(graph.size(), false); // Множество вершин, для которых вычислены кратчайшие пути из начальной.
  61.  
  62. d[A] = 0.0f;
  63. Q.push({ 0.0f, A }); // { Расстояние, Номер_вершины }.
  64. while (!Q.empty())
  65. {
  66. auto v_min = Q.top(); // Взята ближайшая вершина.
  67. Q.pop();
  68.  
  69. int v = v_min.second; // Номер вершины v.
  70. if (isUsed[v])
  71. continue;
  72. isUsed[v] = true;
  73.  
  74. // Для каждой вершины u, смежной с v:
  75. for (pair<float, int> e : graph[v])
  76. {
  77. int u = e.second; // Номер вершины u.
  78. float len_vu = e.first; // Вес ребра со смежной.
  79.  
  80. // Добавление в очередь вершины u, смежной с v.
  81. if (d[u] > d[v] + len_vu)
  82. {
  83. d[u] = d[v] + len_vu;
  84. Q.push({ len_vu, u });
  85. }
  86. }
  87.  
  88. if (isUsed[B])
  89. {
  90. cout << fixed;
  91. cout.precision(3);
  92. cout << d[B] << endl;
  93. break;
  94. }
  95. }
  96.  
  97. if (!isUsed[B])
  98. cout << -1 << endl;
  99. }
  100.  
  101. void main()
  102. {
  103. vector<string> in
  104. {
  105. "input01.txt",
  106. "input02.txt",
  107. "input03.txt",
  108. "input04.txt",
  109. "input05.txt",
  110. "input06.txt",
  111. "input07.txt",
  112. "input08.txt",
  113. "input09.txt",
  114. "input10.txt",
  115. "input11.txt",
  116. "input12.txt",
  117. "input13.txt",
  118. "input14.txt",
  119. "input15.txt",
  120. "input16.txt",
  121. "input17.txt",
  122. "input18.txt",
  123. "input19.txt",
  124. "input20.txt",
  125. "input21.txt",
  126. "input22.txt",
  127. "input23.txt",
  128. "input24.txt",
  129. "input25.txt",
  130. "input26.txt",
  131. "input27.txt",
  132. "input28.txt",
  133. "input29.txt",
  134. "input30.txt",
  135. "input31.txt",
  136. "input32.txt",
  137. };
  138.  
  139. vector<string> out
  140. {
  141. "output01.txt", // 1.571
  142. "output02.txt", // -1
  143. "output03.txt", // 3.142
  144. "output04.txt", // 0.000
  145. "output05.txt", // -1
  146. "output06.txt", // 0.017
  147. "output07.txt", // 0.000
  148. "output08.txt", // -1
  149. "output09.txt", // -1
  150. "output10.txt", // -1
  151. "output11.txt", // 0.868
  152. "output12.txt", // 2.653
  153. "output13.txt", // -1
  154. "output14.txt", // -1
  155. "output15.txt", // -1
  156. "output16.txt", // 3.011
  157. "output17.txt", // -1
  158. "output18.txt", // -1
  159. "output19.txt", // -1
  160. "output20.txt", // 1.957
  161. "output21.txt", // -1
  162. "output22.txt", // -1
  163. "output23.txt", // 2.484
  164. "output24.txt", // 1.346
  165. "output25.txt", // -1
  166. "output26.txt", // 2.915
  167. "output27.txt", // 0.933
  168. "output28.txt", // 2.739
  169. "output29.txt", // -1
  170. "output30.txt", // 2.435
  171. "output31.txt", // 1.668
  172. "output32.txt", // 0.833
  173. };
  174.  
  175. for (auto &obj : in)
  176. test(obj);
  177.  
  178. int y;
  179. }
  180.  
  181.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement