57122023-09-09 18:48:56TomaSajtA lehető legkevesebb metróval utazás (40 pont)cpp17Elfogadva 40/40125ms44420 KiB
#include <bits/stdc++.h>
using namespace std;

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<int> prev_station(m + 1);
  vector<int> reached_with_line(m + 1);
  vector<int> dist(m + 1, -1);
  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_station[next_station] = curr_station;
        reached_with_line[next_station] = line;
        q.push(next_station);
      }
    }
  }

  cout << dist[e] << '\n';
  if (dist[e] == -1) return 0;

  stack<int> res;
  int curr_station = e;
  while (curr_station != s) {
    res.push(reached_with_line[curr_station]);
    curr_station = prev_station[curr_station];
  }
  while (!res.empty()) {
    cout << res.top() << ' ';
    res.pop();
  }
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base40/40
1Elfogadva0/03ms1828 KiB
2Elfogadva0/04ms3516 KiB
3Elfogadva2/23ms2296 KiB
4Elfogadva2/23ms2512 KiB
5Elfogadva2/23ms2944 KiB
6Elfogadva2/22ms2976 KiB
7Elfogadva2/23ms3376 KiB
8Elfogadva2/23ms3840 KiB
9Elfogadva2/24ms4428 KiB
10Elfogadva2/24ms4380 KiB
11Elfogadva2/23ms3968 KiB
12Elfogadva2/24ms4988 KiB
13Elfogadva2/26ms5312 KiB
14Elfogadva2/24ms5416 KiB
15Elfogadva2/2122ms30872 KiB
16Elfogadva2/2120ms35592 KiB
17Elfogadva2/2122ms40368 KiB
18Elfogadva2/2125ms44420 KiB
19Elfogadva2/24ms23612 KiB
20Elfogadva2/24ms24084 KiB
21Elfogadva2/24ms23368 KiB
22Elfogadva2/24ms24220 KiB