10333 | 2024-03-31 17:08:17 | szil | Vázsony vonatjegyet vásárol | cpp17 | Wrong answer 0/100 | 600ms | 123496 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;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 8ms | 13704 KiB | ||||
2 | Wrong answer | 112ms | 34696 KiB | ||||
subtask2 | 0/20 | ||||||
3 | Wrong answer | 54ms | 26696 KiB | ||||
4 | Wrong answer | 330ms | 70140 KiB | ||||
5 | Wrong answer | 246ms | 60020 KiB | ||||
6 | Wrong answer | 216ms | 54664 KiB | ||||
7 | Wrong answer | 250ms | 63264 KiB | ||||
8 | Wrong answer | 204ms | 54188 KiB | ||||
9 | Wrong answer | 465ms | 88656 KiB | ||||
subtask3 | 0/15 | ||||||
10 | Wrong answer | 152ms | 63824 KiB | ||||
11 | Wrong answer | 14ms | 28920 KiB | ||||
12 | Wrong answer | 14ms | 29244 KiB | ||||
13 | Wrong answer | 41ms | 36632 KiB | ||||
14 | Wrong answer | 70ms | 50860 KiB | ||||
subtask4 | 0/20 | ||||||
15 | Wrong answer | 59ms | 42060 KiB | ||||
16 | Wrong answer | 187ms | 59208 KiB | ||||
17 | Wrong answer | 37ms | 41888 KiB | ||||
18 | Wrong answer | 67ms | 48648 KiB | ||||
19 | Wrong answer | 39ms | 42320 KiB | ||||
20 | Wrong answer | 70ms | 48396 KiB | ||||
subtask5 | 0/45 | ||||||
21 | Wrong answer | 7ms | 36264 KiB | ||||
22 | Wrong answer | 600ms | 123496 KiB | ||||
23 | Wrong answer | 45ms | 44156 KiB | ||||
24 | Wrong answer | 182ms | 65672 KiB | ||||
25 | Wrong answer | 172ms | 61268 KiB | ||||
26 | Wrong answer | 296ms | 74540 KiB | ||||
27 | Wrong answer | 175ms | 64628 KiB | ||||
28 | Wrong answer | 455ms | 109128 KiB | ||||
29 | Wrong answer | 108ms | 57296 KiB | ||||
30 | Wrong answer | 405ms | 97784 KiB | ||||
31 | Wrong answer | 194ms | 66036 KiB | ||||
32 | Wrong answer | 43ms | 49776 KiB | ||||
33 | Wrong answer | 312ms | 75972 KiB | ||||
34 | Wrong answer | 261ms | 75136 KiB | ||||
35 | Wrong answer | 289ms | 74008 KiB | ||||
36 | Wrong answer | 282ms | 72304 KiB | ||||
37 | Wrong answer | 254ms | 70956 KiB |