Advertisement
Korotkodul

Untitled

Sep 28th, 2021
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <cmath>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <functional>
  7. #include <map>
  8.  
  9. using namespace std;
  10.  
  11. //метод update для построения графа: компонента связности = станция
  12. //трелки идут от более ранних электричек к более поздним
  13.  
  14. //color: 0 - белый, 1 - серый, 2 - черный
  15. void dfs(int v, vector <vector <int> > & gr, vector <int>& TopSort, vector <int>& color) {
  16. if (color[v] == 2) return;
  17. color[v] = 1;
  18. for (int u : gr[v]) {
  19. dfs(u, gr, TopSort, color);
  20. }
  21. color[v] = 2;
  22. TopSort.push_back(v);
  23. }
  24.  
  25.  
  26. int main()
  27. {
  28. int N;
  29. //cin >> N;
  30. //N = rand() % 10 + 1;
  31. N = 6;
  32. cout << "N = " << N << '\n';
  33. map <int, vector <pair <int, int> > > railway; // double - time through; int - vertex (station)
  34. for (int i = 1; i < N + 1; ++i) {
  35.  
  36. int A, B=0, C, D=0;
  37. //cin >> A >> B >> C >> D;
  38. A = rand() % 10 + 1;
  39. while (B<=A) B = rand() % 10 + 1;
  40. C = rand() % 10 + 1;
  41. D = rand() % 10 + 1;
  42. cout << A << " " << B << " " << C << " " << D << '\n';
  43. int t_A = C;
  44. int t_B = C + (B - A) * D;
  45. cout << "t_A = " << t_A << " t_B = " << t_B << '\n';
  46. //cout << "Tin = " << t_A << " Tout = " << t_B << '\n';
  47. pair <int, int> new_A;
  48. int train = i;
  49. new_A.first = t_A;
  50. new_A.second = train;
  51. //заполняем наш map railway
  52. vector <pair <int, int> > new_vec_A = { new_A };
  53. if (railway.find(A) == railway.end()) railway.insert(make_pair(A, new_vec_A));
  54. else railway[A].push_back({ t_A, train });
  55. pair <int, int> new_B;
  56. new_B.first = t_B;
  57. new_B.second = train;
  58. vector <pair <int, int> > new_vec_B = { new_B };
  59. if (railway.find(B) == railway.end()) railway.insert(make_pair(B, new_vec_B));
  60. else railway[B].push_back({ t_B, train });
  61.  
  62. }cout << '\n';
  63. for (auto el : railway) {
  64. auto station = el.first;
  65. sort(railway[station].begin(), railway[station].end());
  66. }
  67. //теперь создадим граф
  68. vector <vector <int> > gr(N + 1);
  69. for (auto el : railway) {
  70. int station = el.first; //электрички, побывавшие на этой станции, уже отсортированы в порядке воозрастания
  71. int sz = (int)el.second.size();
  72. vector <pair <int, int> > order = el.second;
  73. for (int i = 0; i < sz - 1; ++i) {
  74. int v_from = order[i].second; //побывала на станции раньше
  75. int v_to = order[i + 1].second;
  76. gr[v_from].push_back(v_to);
  77. }
  78. }
  79. //теперь делаем TopSort
  80. vector <int> TopSort;
  81. vector <int> color(N + 1, 0);
  82. for (int i = 1; i < N + 1; ++i) {
  83. dfs(i, gr, TopSort, color);
  84. }
  85. reverse(TopSort.begin(), TopSort.end());
  86. for (int i = 0; i < TopSort.size(); ++i) {
  87. cout << TopSort[i] << ' ';
  88. }cout << '\n';
  89. /*for (int i = 1; i < gr.size(); ++i) {
  90. cout << "from = " <<i<< '\n' << "to = " << '\n';
  91. for (int j = 0; j < gr[i].size(); ++j) {
  92. cout<<gr[i][j] << ' ';
  93. }cout << '\n';
  94. }*/
  95.  
  96. /*for (auto el : railway) {
  97. cout << "station = " << el.first << '\n';
  98. cout << "info = " << '\n';
  99. for (auto to : el.second) cout << to.first <<" "<<to.second << '\n';
  100. cout << '\n';
  101. }*/
  102. cout << "done\n";
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement