50502023-04-11 20:26:41CattA lehető legkevesebb átszállás (50 pont)cpp17Time limit exceeded 0/50277ms166268 KiB
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

const int INF = 1e9;

int main() {
    freopen("../input.txt", "r", stdin);
    freopen("../output.txt", "w", stdout);
    int n, m;
    cin >> n >> m;

    // a gráf megadása
    vector<vector<int>> graph(m + 1);
    for (int i = 1; i <= n; i++) {
        int a, b;
        cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    // BFS algoritmus az átszállások számának meghatározására
    vector<int> dist(m + 1, INF);
    vector<int> prev(m + 1, -1);
    queue<int> q;
    q.push(1);
    dist[1] = 0;
    while (!q.empty()) {
        int u = q.front(); q.pop();
        for (int v : graph[u]) {
            if (dist[v] == INF) {
                dist[v] = dist[u] + 1;
                prev[v] = u;
                q.push(v);
            }
        }
    }

    // az eredmény kiszámítása
    if (dist[m] == INF) {
        cout << "-1\n";
    } else {
        cout << dist[m] / 2 << "\n";
        vector<int> path;
        int u = m;
        while (u != -1) {
            path.push_back(u);
            u = prev[u];
        }
        for (int i = path.size() - 1; i >= 0; i--) {
            cout << path[i] << " ";
        }
    }
    cout << endl;

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base0/50
1Time limit exceeded0/0277ms166268 KiB
2Runtime error0/032ms64808 KiB
3Runtime error0/134ms64520 KiB
4Runtime error0/130ms64316 KiB
5Runtime error0/232ms64304 KiB
6Runtime error0/228ms64068 KiB
7Runtime error0/228ms63836 KiB
8Runtime error0/232ms63676 KiB
9Runtime error0/228ms63580 KiB
10Runtime error0/232ms63576 KiB
11Runtime error0/227ms63348 KiB
12Runtime error0/227ms63148 KiB
13Runtime error0/230ms62928 KiB
14Runtime error0/226ms62688 KiB
15Runtime error0/232ms62692 KiB
16Runtime error0/226ms62540 KiB
17Runtime error0/232ms62368 KiB
18Runtime error0/232ms62296 KiB
19Runtime error0/232ms62304 KiB
20Runtime error0/232ms62304 KiB
21Runtime error0/226ms62220 KiB
22Runtime error0/232ms62212 KiB
23Runtime error0/232ms62116 KiB
24Runtime error0/230ms61892 KiB
25Runtime error0/230ms61900 KiB
26Runtime error0/230ms61880 KiB
27Runtime error0/230ms61888 KiB
28Runtime error0/230ms61664 KiB