49152023-04-07 00:18:02TomaSajtA lehető legkevesebb metróval utazás (40 pont)cpp17Time limit exceeded 32/40583ms13856 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  cin.tie(0), ios::sync_with_stdio(0);
  int n, m, s, e;
  cin >> n >> m >> s >> e;
  vector<vector<int>> station_to_lines(m + 1);
  vector<vector<int>> line_to_stations(n + 1);
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    while (a--) {
      int b;
      cin >> b;
      line_to_stations[i].push_back(b);
      station_to_lines[b].push_back(i);
    }
  }
  vector<array<int, 2>> prev(m + 1);
  vector<int> dist(m + 1, -1);
  prev[s] = {-1, -1}; // {station,line}
  dist[s] = 0;
  queue<int> q;
  q.push(s);
  while (!q.empty()) {
    int curr_station = q.front();
    q.pop();
    for (int &line : station_to_lines[curr_station]) {
      for (int &next_station : line_to_stations[line]) {
        if (dist[next_station] != -1)
          continue;
        dist[next_station] = dist[curr_station] + 1;
        prev[next_station] = {curr_station, line};
        q.push(next_station);
      }
    }
  }
  cout << dist[e] << endl;
  if (dist[e] == -1)
    return 0;

  stack<int> res;
  array<int, 2> currState = prev[e];
  while (currState[0] != -1) {
    res.push(currState[1]);
    currState = prev[currState[0]];
  }
  while (!res.empty()) {
    cout << res.top() << ' ';
    res.pop();
  }
}
SubtaskSumTestVerdictTimeMemory
base32/40
1Accepted0/03ms1832 KiB
2Accepted0/06ms3384 KiB
3Accepted2/23ms2232 KiB
4Accepted2/22ms2316 KiB
5Accepted2/23ms2400 KiB
6Accepted2/22ms2324 KiB
7Accepted2/24ms2896 KiB
8Accepted2/24ms3248 KiB
9Accepted2/28ms3468 KiB
10Accepted2/26ms3528 KiB
11Accepted2/23ms3040 KiB
12Accepted2/27ms4216 KiB
13Accepted2/27ms4432 KiB
14Accepted2/26ms4532 KiB
15Time limit exceeded0/2566ms13544 KiB
16Time limit exceeded0/2583ms13448 KiB
17Time limit exceeded0/2579ms13608 KiB
18Time limit exceeded0/2568ms13856 KiB
19Accepted2/24ms4436 KiB
20Accepted2/26ms5044 KiB
21Accepted2/24ms4360 KiB
22Accepted2/26ms5028 KiB