91732024-02-16 19:30:39mraronA lehető legkevesebb metróval utazás (40 pont)cpp17Futási hiba 30/40273ms64156 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1e9;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m, start, end;
    cin >> n >> m >> start >> end;
    vector<vector<int>> metro(n);
    vector<vector<int>> adj(m + 1);
    for (int i = 0; i < n; i++)
    {
        int a;
        cin >> a;
        metro[i].resize(a);
        for (int j = 0; j < a; j++)
        {
            int b;
            cin >> b;
            adj[b].push_back(i);
            metro[i].push_back(b);
        }
    }
    priority_queue<array<int, 4>, vector<array<int, 4>>, greater<array<int, 4>>> q;
    q.push({0, start, -2, -2});
    vector<int> dist(m + 1, INF);
    vector<pair<int, int>> parent(m + 1);
    while (!q.empty())
    {
        auto [d, v, p, met] = q.top();
        q.pop();
        if (dist[v] != INF)
        {
            continue;
        }
        dist[v] = d;
        parent[v] = {p, met};

        for (int i : adj[v])
        {
            for (int j : metro[i])
            {
                if (dist[j] > dist[v] + 1)
                {
                    array<int, 4> pb;
                    pb[0] = dist[v] + 1;
                    pb[1] = j;
                    pb[2] = v;
                    pb[3] = i;
                    q.push(pb);
                }
            }
        }
    }
    
    if (dist[end] == INF)
    {
        cout << "-1";
        return 0;
    }
    cout << dist[end] << "\n";
    vector<int> ans;
    while (true)
    {
        if(parent[end].second==-2) break;
        ans.push_back(parent[end].second);
        end = parent[end].first;
        
    }
    reverse(ans.begin(), ans.end());
    
    for (int i : ans)
        cout << i + 1 << " ";
}

RészfeladatÖsszpontTesztVerdiktIdőMemória
base30/40
1Elfogadva0/03ms1752 KiB
2Elfogadva0/093ms4788 KiB
3Elfogadva2/23ms2068 KiB
4Elfogadva2/23ms2116 KiB
5Elfogadva2/24ms2912 KiB
6Elfogadva2/23ms2468 KiB
7Elfogadva2/2144ms7088 KiB
8Elfogadva2/2240ms36264 KiB
9Futási hiba0/270ms64156 KiB
10Elfogadva2/2273ms36656 KiB
11Elfogadva2/228ms7684 KiB
12Elfogadva2/2125ms7048 KiB
13Elfogadva2/2127ms7024 KiB
14Elfogadva2/2104ms7204 KiB
15Futási hiba0/2175ms63432 KiB
16Futási hiba0/2175ms63456 KiB
17Futási hiba0/2174ms63428 KiB
18Futási hiba0/2175ms63336 KiB
19Elfogadva2/241ms5052 KiB
20Elfogadva2/278ms6068 KiB
21Elfogadva2/216ms4676 KiB
22Elfogadva2/292ms6480 KiB