94862024-02-22 11:15:40csaron71A lehető legkevesebb metróval utazás (40 pont)cpp17Wrong answer 24/4061ms63552 KiB
#include <bits/stdc++.h>
using namespace std;

int main() {
    int n, m, kezd, veg;
    cin >> n >> m >> kezd >> veg;
    kezd--;
    veg--;
    vector<vector<pair<int, int> > > graph(m, vector<pair<int, int> >({}));
    for (int i=0; i<n; i++) {
        int x;
        cin >> x;
        vector<int> epp;
        for (int j=0; j<x; j++) {
            int y;
            cin >> y;
            y--;
            epp.push_back(y);
        }
        for (int q=0; q<x; q++) {
            for (int j=q+1; j<x; j++) {
                graph[epp[q]].push_back({epp[j], i+1});
                graph[epp[j]].push_back({epp[q], i+1});
            }
        }
    }

    queue<pair<pair<int, int>, pair<int, int> > > sor; // melyik, milyen messze, honnan, melyik metro
    vector<bool> volt(m, false);
    vector<int> tav(m, -1);
    sor.push({{kezd, 0}, {0, 0}});
    tav[kezd]=0;
    vector<int> honnan(m);
    vector<int> metro(m);

    while (sor.size()>0) {
        int elso=sor.front().first.first;
        int hany=sor.front().first.second;
        int mibol=sor.front().second.first;
        int melyik=sor.front().second.second;
        sor.pop();
        if (volt[elso]==true) continue;
        tav[elso]=hany;
        volt[elso]=true;
        honnan[elso]=mibol;
        metro[elso]=melyik;

        for (pair<int, int> sz : graph[elso]) {
            if (volt[sz.first]!=true) {
                sor.push({{sz.first, hany+1}, {elso, sz.second}});
            }
        }
    }

    cout << tav[veg] << "\n";

    vector<int> vege;
    int most=veg;
    while (most!=kezd) {
        //cout << most << " ";
        vege.push_back(metro[most]);
        most=honnan[most];
    }
    //cout << "\n";
    reverse(vege.begin(), vege.end());
    for (int sz : vege) {
        cout << sz << " ";
    }
}
SubtaskSumTestVerdictTimeMemory
base24/40
1Accepted0/03ms1816 KiB
2Accepted0/039ms24948 KiB
3Accepted2/23ms2224 KiB
4Accepted2/23ms2448 KiB
5Accepted2/28ms8440 KiB
6Wrong answer0/23ms2968 KiB
7Accepted2/241ms34540 KiB
8Runtime error0/259ms63552 KiB
9Runtime error0/259ms63480 KiB
10Runtime error0/259ms63452 KiB
11Accepted2/212ms11732 KiB
12Accepted2/246ms33284 KiB
13Accepted2/246ms32856 KiB
14Accepted2/241ms29156 KiB
15Runtime error0/261ms63040 KiB
16Runtime error0/259ms63032 KiB
17Runtime error0/259ms63020 KiB
18Runtime error0/259ms63000 KiB
19Accepted2/219ms13808 KiB
20Accepted2/232ms23064 KiB
21Accepted2/29ms7788 KiB
22Accepted2/239ms26720 KiB