94762024-02-22 10:32:09csaron71A lehető legkevesebb metróval utazás (40 pont)cpp17Runtime error 2/4068ms63576 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 i=0; i<x; i++) {
            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(n);
    vector<int> metro(n);

    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
base2/40
1Runtime error0/03ms2064 KiB
2Runtime error0/028ms24708 KiB
3Runtime error0/23ms2560 KiB
4Runtime error0/23ms2604 KiB
5Accepted2/28ms8508 KiB
6Runtime error0/23ms2948 KiB
7Runtime error0/228ms32020 KiB
8Runtime error0/248ms55040 KiB
9Runtime error0/259ms63576 KiB
10Runtime error0/261ms63564 KiB
11Runtime error0/213ms11632 KiB
12Runtime error0/234ms31112 KiB
13Runtime error0/235ms31484 KiB
14Runtime error0/243ms29464 KiB
15Runtime error0/263ms62916 KiB
16Runtime error0/259ms62892 KiB
17Runtime error0/265ms62876 KiB
18Runtime error0/268ms62644 KiB
19Runtime error0/216ms14160 KiB
20Runtime error0/226ms23160 KiB
21Runtime error0/28ms8068 KiB
22Runtime error0/241ms27248 KiB