Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <algorithm>
- #include <vector>
- #include <functional>
- #include <map>
- using namespace std;
- //метод update для построения графа: компонента связности = станция
- //трелки идут от более ранних электричек к более поздним
- //color: 0 - белый, 1 - серый, 2 - черный
- void dfs(int v, vector <vector <int> > & gr, vector <int>& TopSort, vector <int>& color) {
- if (color[v] == 2) return;
- color[v] = 1;
- for (int u : gr[v]) {
- dfs(u, gr, TopSort, color);
- }
- color[v] = 2;
- TopSort.push_back(v);
- }
- int main()
- {
- int N;
- cin >> N;
- map <int, vector <pair <int, int> > > railway; // double - time through; int - vertex (station)
- for (int i = 1; i < N + 1; ++i) {
- int A, B, C, D;
- cin >> A >> B >> C >> D;
- int t_A = C;
- int t_B = C + (B-A) * D;
- //cout << "Tin = " << t_A << " Tout = " << t_B << '\n';
- pair <int, int> new_A;
- int train = i;
- new_A.first = t_A;
- new_A.second = train;
- //хаполняем наш map railway
- vector <pair <int, int> > new_vec_A = { new_A };
- if (railway.find(A) == railway.end()) railway.insert(pair <int, vector <pair <int, int> > > (A, new_vec_A ));
- else railway[A].push_back({ t_A, train });
- pair <int, int> new_B;
- new_B.first = t_B;
- new_B.second = train;
- vector <pair <int, int> > new_vec_B = { new_B };
- if (railway.find(B) == railway.end()) railway.insert(pair <int, vector <pair <int, int> > >(B, new_vec_B));
- else railway[B].push_back({ t_B, train });
- }
- for (auto el : railway) {
- auto station = el.first;
- sort(railway[station].begin(), railway[station].end());
- }
- //теперь создадим граф
- vector <vector <int> > gr(N + 1);
- for (auto el : railway) {
- int station = el.first; //электрички, побывавшие на этой станции, уже отсортированы в порядке воозрастания
- int sz = (int)el.second.size();
- vector <pair <int, int> > order = el.second;
- for (int i = 0; i < sz - 1; ++i) {
- int v_from = order[i].second; //побывала на станции раньше
- int v_to = order[i + 1].second;
- gr[v_from].push_back(v_to);
- }
- }
- //теперь делаем TopSort
- vector <int> TopSort;
- vector <int> color(N + 1, 0);
- for (int i = 1; i < N + 1; ++i) {
- dfs(i, gr, TopSort, color);
- }
- reverse(TopSort.begin(), TopSort.end());
- for (int i = 0; i < TopSort.size(); ++i) {
- cout << TopSort[i] << ' ';
- }cout << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement