91762024-02-17 14:29:20xxxA lehető legkevesebb metróval utazás (40 pont)cpp17Runtime error 30/40574ms64244 KiB
#include <bits/stdc++.h>
using namespace std;

int main() {
	int n, m, ind, erk;
	cin >> n >> m >> ind >> erk;
	vector<vector<int> > megall(m+1);
	vector<vector<bool> > el(n, vector<bool>(n, false));
	for(int i = 0; i < n; i++) {
		int db;
		cin >> db;
		for(int j = 0; j < db; j++) {
			int x;
			cin >> x;
			megall[x].push_back(i);
		}
	}

	

	for(int i = 1; i <= m; i++) {
		if (megall[i].size() > 1) {
			for(int j = 0; j < megall[i].size(); j++) {
				for(int k = j+1; k < megall[i].size(); k++) {
					el[megall[i][j]][megall[i][k]] = 1;
					el[megall[i][k]][megall[i][j]] = 1;
				}
			}
		}
	}

	queue<int> q;
	vector<int> tav(n+1, -1), parent(n+1, -1);

	for(int i : megall[ind]) {
		q.push(i);
		tav[i] = 1;
	}

	while(!q.empty()) {
		int u = q.front();
		q.pop();
		for(int i = 0; i < n; i++) {
			if (el[u][i]) {
				if (tav[i] == -1) {
					tav[i] = tav[u]+1;
					parent[i] = u;
					q.push(i);
				}
			}
		}
	}

	int ans = INT_MAX, mini = 0;
	for(int i : megall[erk]) {
		if (ans > tav[i]) {
			ans = tav[i];
			mini = i;
		}
	}
	cout << ans << '\n';
	
	vector<int> ansv;
	
	while(ans--) {
		ansv.push_back(mini);
		mini = parent[mini];
	}
	reverse(ansv.begin(), ansv.end());
	for(int x : ansv) {
		cout << x+1 << ' ';
	}

	return 0;
}
SubtaskSumTestVerdictTimeMemory
base30/40
1Accepted0/03ms2084 KiB
2Accepted0/07ms3124 KiB
3Accepted2/23ms2440 KiB
4Accepted2/23ms2472 KiB
5Accepted2/23ms2776 KiB
6Runtime error0/261ms64244 KiB
7Accepted2/24ms3228 KiB
8Accepted2/24ms3344 KiB
9Accepted2/24ms3624 KiB
10Accepted2/24ms3740 KiB
11Accepted2/24ms3576 KiB
12Accepted2/28ms4176 KiB
13Accepted2/28ms4176 KiB
14Accepted2/27ms4172 KiB
15Time limit exceeded0/2549ms8884 KiB
16Time limit exceeded0/2565ms8852 KiB
17Time limit exceeded0/2569ms8872 KiB
18Time limit exceeded0/2574ms8884 KiB
19Accepted2/26ms4256 KiB
20Accepted2/27ms4488 KiB
21Accepted2/24ms4108 KiB
22Accepted2/27ms4588 KiB