103332024-03-31 17:08:17szilVázsony vonatjegyet vásárolcpp17Wrong answer 0/100600ms123496 KiB
#include <bits/stdc++.h>
#include <queue>

using namespace std;
using ll = long long;

const int MAXN = 100'001;

vector<pair<int, ll>> g[MAXN];
vector<int> g2[MAXN];
int dp[MAXN], n, m, a, b;
ll dist[MAXN];

void dijkstra(int source) {
	fill(dist, dist+MAXN, 1e18);
	priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
	dist[source] = 0;
	pq.push({0, source});
	while (!pq.empty()) {
		auto [d, u] = pq.top(); pq.pop();
		if (dist[u] != d) continue;
		for (auto [v, w] : g[u]) {
			if (dist[v] > dist[u] + w) {
				dist[v] = dist[u] + w;
				pq.push({dist[v], v});
			}
		}
	}
}

void build_graph() {
	for (int i = 1; i <= n; i++) {
		for (auto [u, w] : g[i]) {
			if (dist[i] >= dist[u]) {
				g2[i].emplace_back(u);
			}
		}
	}
}

int longest_route(int u) {
	if (dp[u] == -1) {
		dp[u] = INT_MIN;
		for (int v : g2[u]) {
			dp[u] = max(dp[u], 1 + longest_route(v));
		}
	}
	return dp[u];
}

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	fill(dp, dp+MAXN, -1);
	cin >> n >> m >> a >> b;
	for (int i = 0; i < m; i++) {
		int u, v; ll w; cin >> u >> v >> w;
		g[u].emplace_back(v, w);
		g[v].emplace_back(u, w);
	}
	dp[b] = 0;
	dijkstra(b);
	build_graph();
	cout << longest_route(a) << "\n";
	int u = a;
	while (u != b) {
		pair<int, int> best = {-1, 0};
		dp[u] = INT_MIN;
		cout << u << " ";
		for (int v : g2[u]) {
			best = max(best, {dp[v], v});
		}
		u = best.second;
	}
	cout << b << "\n";
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted8ms13704 KiB
2Wrong answer112ms34696 KiB
subtask20/20
3Wrong answer54ms26696 KiB
4Wrong answer330ms70140 KiB
5Wrong answer246ms60020 KiB
6Wrong answer216ms54664 KiB
7Wrong answer250ms63264 KiB
8Wrong answer204ms54188 KiB
9Wrong answer465ms88656 KiB
subtask30/15
10Wrong answer152ms63824 KiB
11Wrong answer14ms28920 KiB
12Wrong answer14ms29244 KiB
13Wrong answer41ms36632 KiB
14Wrong answer70ms50860 KiB
subtask40/20
15Wrong answer59ms42060 KiB
16Wrong answer187ms59208 KiB
17Wrong answer37ms41888 KiB
18Wrong answer67ms48648 KiB
19Wrong answer39ms42320 KiB
20Wrong answer70ms48396 KiB
subtask50/45
21Wrong answer7ms36264 KiB
22Wrong answer600ms123496 KiB
23Wrong answer45ms44156 KiB
24Wrong answer182ms65672 KiB
25Wrong answer172ms61268 KiB
26Wrong answer296ms74540 KiB
27Wrong answer175ms64628 KiB
28Wrong answer455ms109128 KiB
29Wrong answer108ms57296 KiB
30Wrong answer405ms97784 KiB
31Wrong answer194ms66036 KiB
32Wrong answer43ms49776 KiB
33Wrong answer312ms75972 KiB
34Wrong answer261ms75136 KiB
35Wrong answer289ms74008 KiB
36Wrong answer282ms72304 KiB
37Wrong answer254ms70956 KiB