124832024-12-19 08:39:56feheristvanA lehető legkevesebb átszállás (50 pont)cpp17Hibás válasz 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base2/50
1Hibás válasz0/01ms496 KiB
2Hibás válasz0/012ms3384 KiB
3Elfogadva1/11ms320 KiB
4Elfogadva1/11ms320 KiB
5Hibás válasz0/21ms320 KiB
6Hibás válasz0/21ms320 KiB
7Hibás válasz0/22ms568 KiB
8Hibás válasz0/22ms420 KiB
9Hibás válasz0/22ms568 KiB
10Hibás válasz0/23ms824 KiB
11Hibás válasz0/24ms1592 KiB
12Hibás válasz0/26ms1592 KiB
13Hibás válasz0/22ms1160 KiB
14Hibás válasz0/23ms1592 KiB
15Hibás válasz0/24ms1932 KiB
16Hibás válasz0/26ms2000 KiB
17Hibás válasz0/28ms2872 KiB
18Hibás válasz0/28ms3128 KiB
19Hibás válasz0/28ms3192 KiB
20Hibás válasz0/210ms3384 KiB
21Hibás válasz0/210ms3384 KiB
22Hibás válasz0/210ms3572 KiB
23Futási hiba0/29ms2816 KiB
24Futási hiba0/29ms2936 KiB
25Futási hiba0/29ms3384 KiB
26Futási hiba0/29ms3384 KiB
27Futási hiba0/29ms3640 KiB
28Futási hiba0/210ms3640 KiB