5048 | 2023. 04. 11 20:18:54 | Catt | A lehető legkevesebb átszállás (50 pont) | cpp17 | Hibás válasz 2/50 | 13ms | 11520 KiB |
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int INF = 1e9; // Infinite distance value
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> adj(m+1); // Adjacency list
for (int i = 1; i <= n; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(i); // Add train to adjacency list of starting station
adj[b].push_back(-i); // Add train (with negative index) to adjacency list of ending station
}
vector<int> dist(m+1, INF); // Initialize distance array with infinite distance
vector<int> prev(m+1, -1); // Initialize previous node array with -1
queue<int> q;
dist[1] = 0; // Starting station has 0 distance
q.push(1);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v : adj[u]) {
int w = 1; // Weight of edge is 1, since it represents changing trains
if (v < 0) { // If v is a train arriving at the ending station
v = -v; // Get the index of the train
w = 0; // The weight of the edge is 0, since the train is already at the ending station
}
if (dist[u] + w < dist[v]) {
dist[v] = dist[u] + w;
prev[v] = u;
q.push(v);
}
}
}
if (dist[m] == INF) {
cout << -1 << endl; // If it is not possible to reach the ending station
} else {
cout << dist[m] << endl; // Minimum number of changes to reach the ending station
vector<int> path;
int u = m;
while (u != -1) { // Reconstruct the path from the ending station to the starting station
path.push_back(u);
u = prev[u];
}
for (int i = path.size()-1; i >= 0; i--) {
if (path[i] > 0) {
cout << path[i] << " "; // Print the index of the train
}
}
cout << endl;
}
return 0;
}
Részfeladat | Összpont | Teszt | Verdikt | Idő | Memória | ||
---|---|---|---|---|---|---|---|
base | 2/50 | ||||||
1 | Hibás válasz | 0/0 | 3ms | 1808 KiB | |||
2 | Hibás válasz | 0/0 | 13ms | 9328 KiB | |||
3 | Elfogadva | 1/1 | 2ms | 2188 KiB | |||
4 | Elfogadva | 1/1 | 2ms | 2424 KiB | |||
5 | Hibás válasz | 0/2 | 2ms | 2504 KiB | |||
6 | Hibás válasz | 0/2 | 2ms | 2708 KiB | |||
7 | Hibás válasz | 0/2 | 4ms | 3272 KiB | |||
8 | Hibás válasz | 0/2 | 4ms | 3348 KiB | |||
9 | Hibás válasz | 0/2 | 4ms | 3676 KiB | |||
10 | Hibás válasz | 0/2 | 4ms | 4300 KiB | |||
11 | Hibás válasz | 0/2 | 7ms | 6128 KiB | |||
12 | Hibás válasz | 0/2 | 8ms | 6412 KiB | |||
13 | Hibás válasz | 0/2 | 4ms | 4884 KiB | |||
14 | Hibás válasz | 0/2 | 4ms | 6084 KiB | |||
15 | Hibás válasz | 0/2 | 6ms | 6932 KiB | |||
16 | Hibás válasz | 0/2 | 8ms | 7072 KiB | |||
17 | Hibás válasz | 0/2 | 9ms | 9648 KiB | |||
18 | Hibás válasz | 0/2 | 10ms | 9848 KiB | |||
19 | Hibás válasz | 0/2 | 13ms | 10496 KiB | |||
20 | Hibás válasz | 0/2 | 13ms | 11100 KiB | |||
21 | Hibás válasz | 0/2 | 13ms | 11172 KiB | |||
22 | Hibás válasz | 0/2 | 13ms | 11300 KiB | |||
23 | Hibás válasz | 0/2 | 12ms | 9300 KiB | |||
24 | Hibás válasz | 0/2 | 12ms | 10348 KiB | |||
25 | Hibás válasz | 0/2 | 13ms | 10788 KiB | |||
26 | Hibás válasz | 0/2 | 13ms | 11440 KiB | |||
27 | Hibás válasz | 0/2 | 13ms | 11520 KiB | |||
28 | Hibás válasz | 0/2 | 13ms | 11520 KiB |