91522024-02-15 22:35:43UVinceA lehető legkevesebb metróval utazás (40 pont)cpp17Wrong answer 12/40257ms64632 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

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, INT_MAX);
    vector<pair<int, int>> parent(m + 1);
    while (!q.empty())
    {
        auto [d, v, p, met] = q.top();
        q.pop();
        if (dist[v] != INT_MAX)
        {
            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] == INT_MAX)
    {
        cout << "-1";
        return 0;
    }
    cout << dist[end] << "\n";
    vector<int> ans;
    while (end != 1)
    {
        ans.push_back(parent[end].second);
        end = parent[end].first;
    }
    reverse(ans.begin(), ans.end());
    for (int i : ans)
        cout << i + 1 << " ";
}
SubtaskSumTestVerdictTimeMemory
base12/40
1Accepted0/03ms1824 KiB
2Wrong answer0/093ms4860 KiB
3Wrong answer0/23ms2268 KiB
4Runtime error0/259ms64632 KiB
5Runtime error0/24ms3284 KiB
6Accepted2/23ms2552 KiB
7Runtime error0/2195ms64392 KiB
8Partially correct1/2233ms35868 KiB
9Runtime error0/268ms64284 KiB
10Partially correct1/2257ms36312 KiB
11Partially correct1/227ms7096 KiB
12Partially correct1/2123ms6548 KiB
13Partially correct1/2125ms6616 KiB
14Partially correct1/2101ms6580 KiB
15Runtime error0/2178ms64048 KiB
16Runtime error0/2178ms63816 KiB
17Runtime error0/2155ms63816 KiB
18Runtime error0/2155ms63816 KiB
19Partially correct1/241ms4592 KiB
20Partially correct1/276ms5604 KiB
21Partially correct1/216ms3960 KiB
22Partially correct1/290ms6016 KiB