4916 2023. 04. 07 00:19:53 TomaSajt A lehető legkevesebb metróval utazás (40 pont) cpp17 Elfogadva 40/40 114ms 25600 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<bool> used_line(n + 1);
  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]) {
      if (used_line[line])
        continue;
      used_line[line] = 1;
      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();
  }
}
Részfeladat Összpont Teszt Verdikt Idő Memória
base 40/40
1 Elfogadva 0/0 3ms 1828 KiB
2 Elfogadva 0/0 4ms 3392 KiB
3 Elfogadva 2/2 3ms 2264 KiB
4 Elfogadva 2/2 3ms 2480 KiB
5 Elfogadva 2/2 3ms 2916 KiB
6 Elfogadva 2/2 3ms 2964 KiB
7 Elfogadva 2/2 3ms 3444 KiB
8 Elfogadva 2/2 3ms 3680 KiB
9 Elfogadva 2/2 4ms 4032 KiB
10 Elfogadva 2/2 4ms 3964 KiB
11 Elfogadva 2/2 3ms 3760 KiB
12 Elfogadva 2/2 4ms 4672 KiB
13 Elfogadva 2/2 4ms 4816 KiB
14 Elfogadva 2/2 4ms 4720 KiB
15 Elfogadva 2/2 114ms 25264 KiB
16 Elfogadva 2/2 111ms 25464 KiB
17 Elfogadva 2/2 114ms 25348 KiB
18 Elfogadva 2/2 111ms 25600 KiB
19 Elfogadva 2/2 4ms 4512 KiB
20 Elfogadva 2/2 4ms 4764 KiB
21 Elfogadva 2/2 4ms 4384 KiB
22 Elfogadva 2/2 4ms 5236 KiB