Advertisement
Korotkodul

C_Электрички_N^2

Oct 5th, 2021
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <functional>
  6. #include <map>
  7. #include <set>
  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 || color[v] == 1) 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.     ios_base::sync_with_stdio(false);
  29.     cin.tie(0);
  30.     int N;
  31.     cin >> N;
  32.     vector < pair <pair <int, int> , pair <int, int> > > TRAIN(N+1); //A {station, time}, B {station, time}
  33.     for (int i = 1; i < N + 1; ++i) {
  34.         int from, to, from_time, pace, to_time;
  35.         cin >> from >> to >> from_time >> pace;
  36.         to_time = (to - from) * pace + from_time;
  37.         pair <int, int> A = { from, from_time };
  38.         pair <int, int> B = { to, to_time };
  39.         TRAIN[i] = { A,B };
  40.     }
  41.     /*cout << '\n';
  42.     for (auto x : TRAIN) {
  43.         auto A = x.first;
  44.         auto B = x.second;
  45.         cout << A.first << ' ' << A.second << '\n';
  46.         cout << B.first << ' ' << B.second << '\n' << '\n';
  47.     }cout << '\n';*/
  48.     vector <vector <pair <int, int> > > STAT; //stations
  49.     set <int> used_stat;
  50.     for (int i = 1; i < N+1; ++i) {
  51.         auto A = TRAIN[i].first;
  52.         int from = A.first;
  53.         if (used_stat.find(from) == used_stat.end()) {
  54.             vector <pair <int, int> > stat_from;
  55.             int from_time = A.second;
  56.             stat_from.push_back({from_time, i });
  57.             for (int j = 1; j < N + 1 && j != i; ++j) {
  58.                 auto A_some = TRAIN[j].first;
  59.                 int from_some = A.first;
  60.                 int from_time_some = A.second;
  61.                 if (from_some == from && used_stat.find(from_some) == used_stat.end()) {
  62.                     stat_from.push_back({ from_time_some, from_some });
  63.                 }
  64.                 auto B_some = TRAIN[j].second;
  65.                 int to_some = B_some.first;
  66.                 int to_time_some = B_some.second;
  67.                 if (to_some == from && used_stat.find(to_some) == used_stat.end()) {
  68.                     stat_from.push_back({ to_time_some, to_some });
  69.                 }
  70.             }
  71.             sort(stat_from.begin(), stat_from.end());
  72.             STAT.push_back(stat_from);
  73.             used_stat.insert(from);
  74.         }
  75.         auto B = TRAIN[i].second;
  76.         int to = B.first;
  77.         if (used_stat.find(to) == used_stat.end()) {
  78.             vector <pair <int, int> > stat_to;
  79.             int to_time = B.second;
  80.             stat_to.push_back({ to_time, i });
  81.             for (int j = 1; j < N + 1 && j != i; ++j) {
  82.                 auto A_some = TRAIN[j].first;
  83.                 int from_some = A_some.first;
  84.                 int from_time_some = A.second;
  85.                 if (from_some == to && used_stat.find(from_some) == used_stat.end()) {
  86.                     stat_to.push_back({ from_time_some, from_some });
  87.                 }
  88.                 auto B_some = TRAIN[j].second;
  89.                 int to_some = B.first;
  90.                 int to_time_some = B.second;
  91.                 if (to_some == to && used_stat.find(to_some) == used_stat.end()) {
  92.                     stat_to.push_back({ to_time_some, to_some });
  93.                 }
  94.             }
  95.             sort(stat_to.begin(), stat_to.end());
  96.             STAT.push_back(stat_to);
  97.             used_stat.insert(to);
  98.         }
  99.     }
  100.     cout << "ONE\n";
  101.     vector <vector <int> > gr(N+1);
  102.     for (int i = 0; i < STAT.size(); ++i) {
  103.         for (int j = 0; j < (int)STAT[i].size() - 1; ++j) {
  104.             int faster = STAT[i][j].second;
  105.             int  slower = STAT[i][j + 1].second;
  106.             cout << "slower = " << slower << '\n';
  107.             gr[faster].push_back(slower);
  108.         }
  109.     }
  110.     for (int i = 1; i < N + 1; ++i) {
  111.         for (auto j : gr[i]) {
  112.             cout << j << ' ';
  113.         }cout << '\n';
  114.     }cout << '\n';
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement