Submission
Status:
PPPPP-PPP-
Subtask/Task Score:
80/100
Score: 80
User: konthaina_TH
Problemset: Fast Delivery
Language: cpp
Time: 0.002 second
Submitted On: 2026-03-06 07:57:26
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int INF = 1e9;
int main() {
ll n, m;
cin >> n >> m;
vector<pair<ll, ll>> adj[n + 1];
vector<ll> parent(n + 1, -1);
for (ll i = 0; i < m; i++) {
ll u, v, w;
cin >> u >> v >> w;
adj[u].push_back({v, w});
}
ll start;
cin >> start;
vector<ll> dist(n + 1, INF);
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
dist[start] = 0;
pq.push({0, start});
ll cnt = 1;
while (!pq.empty()) {
ll d = pq.top().first;
ll u = pq.top().second;
pq.pop();
if (d > dist[u]) continue;
for (auto edge : adj[u]) {
ll v = edge.first;
ll w = edge.second;
if (dist[u] + w < dist[v]) {
dist[v] = dist[u] + w;
parent[v] = u;
pq.push({dist[v], v});
}
}
}
for (ll i = 0; i <= n; i++) {
if (i == start || dist[i] == INF) continue;
if (i != cnt) {
if (cnt == start) {
}
else {
cout << start << " -> " << cnt << " (" << "inf" << ") " << "\n";
cnt = i;
}
}
cout << start << " -> " << i << " (" << dist[i] << ") ";
cnt++;
vector<ll> path;
for (ll curr = i; curr != -1; curr = parent[curr]) {
path.push_back(curr);
}
reverse(path.begin(), path.end());
for (ll node : path) {
cout << node << " ";
}
cout << endl;
}
return 0;
}