94802024-02-22 10:40:57csaron71A lehető legkevesebb metróval utazás (40 pont)cpp17Wrong answer 24/4068ms63968 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/03ms1852 KiB
2Accepted0/039ms25040 KiB
3Accepted2/23ms2364 KiB
4Accepted2/23ms2356 KiB
5Accepted2/28ms8356 KiB
6Wrong answer0/23ms2796 KiB
7Accepted2/241ms34216 KiB
8Runtime error0/259ms63968 KiB
9Runtime error0/259ms63724 KiB
10Runtime error0/261ms63488 KiB
11Accepted2/213ms11700 KiB
12Accepted2/250ms33004 KiB
13Accepted2/248ms33076 KiB
14Accepted2/241ms29584 KiB
15Runtime error0/268ms62760 KiB
16Runtime error0/259ms62752 KiB
17Runtime error0/256ms62724 KiB
18Runtime error0/259ms62696 KiB
19Accepted2/221ms14364 KiB
20Accepted2/234ms23580 KiB
21Accepted2/29ms8312 KiB
22Accepted2/239ms27484 KiB