124832024-12-19 08:39:56feheristvanA lehető legkevesebb átszállás (50 pont)cpp17Wrong answer 2/5012ms3640 KiB
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

struct Train {
    int start, end, index;
};

int main() {
    int N, M;
    cin >> N >> M;

    vector<Train> trains(N);
    for (int i = 0; i < N; i++) {
        cin >> trains[i].start >> trains[i].end;
        trains[i].index = i + 1; // Store the train index (1-based)
    }

    // Create a graph where each station points to the trains that can be taken from it
    vector<vector<int>> graph(M + 1);
    for (const auto& train : trains) {
        graph[train.start].push_back(train.index);
    }

    // BFS to find the shortest path from station 1 to station M
    vector<int> transfers(M + 1, -1); // -1 means not reachable
    vector<int> previousTrain(N + 1, -1); // To track the previous train used
    queue<int> q;

    // Start from station 1
    transfers[1] = 0; // 0 transfers to start
    q.push(1);

    while (!q.empty()) {
        int currentStation = q.front();
        q.pop();

        for (int trainIndex : graph[currentStation]) {
            const Train& train = trains[trainIndex - 1]; // Get the train

            // If we can reach the end of this train
            if (transfers[train.end] == -1) { // If not visited
                transfers[train.end] = transfers[currentStation] + 1; // Increment transfers
                previousTrain[train.end] = trainIndex; // Track the train used
                q.push(train.end);
            }
        }
    }

    // Check if we reached station M
    if (transfers[M] == -1) {
        cout << -1 << endl; // Not reachable
    } else {
        // Output the number of transfers
        cout << transfers[M] << endl;

        // Backtrack to find the trains used
        vector<int> trainPath;
        int currentStation = M;
        while (currentStation != 1) {
            int trainIndex = previousTrain[currentStation];
            trainPath.push_back(trainIndex);
            const Train& train = trains[trainIndex - 1];
            currentStation = train.start; // Move to the starting station of the train
        }

        // Reverse the trainPath to get the correct order
        reverse(trainPath.begin(), trainPath.end());

        // Output the train indices
        for (int trainIndex : trainPath) {
            cout << trainIndex << " ";
        }
        cout << endl;
    }

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base2/50
1Wrong answer0/01ms496 KiB
2Wrong answer0/012ms3384 KiB
3Accepted1/11ms320 KiB
4Accepted1/11ms320 KiB
5Wrong answer0/21ms320 KiB
6Wrong answer0/21ms320 KiB
7Wrong answer0/22ms568 KiB
8Wrong answer0/22ms420 KiB
9Wrong answer0/22ms568 KiB
10Wrong answer0/23ms824 KiB
11Wrong answer0/24ms1592 KiB
12Wrong answer0/26ms1592 KiB
13Wrong answer0/22ms1160 KiB
14Wrong answer0/23ms1592 KiB
15Wrong answer0/24ms1932 KiB
16Wrong answer0/26ms2000 KiB
17Wrong answer0/28ms2872 KiB
18Wrong answer0/28ms3128 KiB
19Wrong answer0/28ms3192 KiB
20Wrong answer0/210ms3384 KiB
21Wrong answer0/210ms3384 KiB
22Wrong answer0/210ms3572 KiB
23Runtime error0/29ms2816 KiB
24Runtime error0/29ms2936 KiB
25Runtime error0/29ms3384 KiB
26Runtime error0/29ms3384 KiB
27Runtime error0/29ms3640 KiB
28Runtime error0/210ms3640 KiB