Advertisement
Korotkodul

Untitled

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