50492023-04-11 20:25:18CattA lehető legkevesebb átszállás (50 pont)cpp17Wrong answer 2/5089ms65004 KiB
#include <iostream>
#include <vector>
#include <queue>
#include <limits>

using namespace std;

const int INF = numeric_limits<int>::max();

struct Edge {
    int dest;
    int train;
};

typedef vector<vector<Edge>> Graph;

vector<int> dijkstra(const Graph& G, int start) {
    int N = G.size();
    vector<int> dist(N, INF);
    vector<int> prev(N, -1);
    priority_queue<pair<int,int>, vector<pair<int,int>>, greater<>> pq;
    dist[start] = 0;
    pq.emplace(0, start);
    while (!pq.empty()) {
        auto [d, u] = pq.top();
        pq.pop();
        if (d > dist[u]) continue;
        for (const auto& e : G[u]) {
            int v = e.dest;
            int w = 1;
            if (dist[v] > dist[u] + w) {
                dist[v] = dist[u] + w;
                prev[v] = e.train;
                pq.emplace(dist[v], v);
            }
        }
    }
    return prev;
}

int main() {
    int N, M;
    cin >> N >> M;

    Graph G(M);
    for (int i = 0; i < N; i++) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        for (int j = a; j < b; j++) {
            G[j].push_back({j+1, i+1});
            G[j+1].push_back({j, i+1});
        }
    }

    auto prev = dijkstra(G, 0);
    if (prev[M-1] == -1) {
        cout << "-1\n";
    } else {
        vector<int> trains;
        int u = M-1;
        while (u != 0) {
            int t = prev[u];
            trains.push_back(t);
            int v = -1;
            for (const auto& e : G[u]) {
                if (e.train == t) {
                    v = e.dest;
                    break;
                }
            }
            u = v;
        }
        int K = trains.size();
        cout << K-1 << endl;
        for (int i = K-1; i >= 0; i--) {
            cout << trains[i] << " ";
        }
        cout << endl;
    }
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base2/50
1Wrong answer0/03ms1684 KiB
2Runtime error0/078ms65004 KiB
3Accepted1/13ms2076 KiB
4Accepted1/13ms2296 KiB
5Wrong answer0/23ms2616 KiB
6Wrong answer0/23ms2708 KiB
7Wrong answer0/217ms14904 KiB
8Wrong answer0/220ms19708 KiB
9Wrong answer0/228ms23448 KiB
10Wrong answer0/241ms35272 KiB
11Wrong answer0/271ms50456 KiB
12Runtime error0/271ms63660 KiB
13Wrong answer0/221ms16008 KiB
14Wrong answer0/245ms28968 KiB
15Wrong answer0/259ms42356 KiB
16Wrong answer0/289ms62072 KiB
17Runtime error0/271ms63208 KiB
18Runtime error0/274ms63188 KiB
19Runtime error0/271ms63160 KiB
20Runtime error0/271ms63140 KiB
21Runtime error0/268ms62904 KiB
22Runtime error0/268ms62672 KiB
23Runtime error0/275ms62668 KiB
24Runtime error0/271ms62636 KiB
25Runtime error0/286ms62608 KiB
26Runtime error0/279ms62612 KiB
27Runtime error0/279ms62388 KiB
28Runtime error0/289ms62216 KiB