Submission

Status:

PPPPPPPPPP

Subtask/Task Score:

100/100

Score: 100

User: Bestzu

Problemset: Fast Delivery

Language: cpp

Time: 0.002 second

Submitted On: 2026-03-13 00:59:13

#include<bits/stdc++.h>
using namespace std;
using pii = pair<int,int>;

int main() {
    ios::sync_with_stdio(false); cin.tie(0);

    int n, m, s; cin >> n >> m;
    vector<vector<pii>> adj(n);
    for(int i = 0; i < m; i++) {
        int u, v, w; cin >> u >> v >> w;
        adj[u].push_back({v, w});
    }
    cin >> s;
    // dijkstra
    const int inf = 1e9 + 7;
    priority_queue<pii, vector<pii>, greater<pii>> pq;
    vector<int> dist(n, inf);
    vector<bool> flag(n, false);
    vector<int> past(n, -1);
    dist[s] = 0;
    pq.push({0, s});
    while(!pq.empty()) {
        int u = pq.top().second;
        pq.pop();
        if(flag[u]) continue;

        flag[u] = true;
        for(auto [v, w] : adj[u]) {
            if(dist[v] > dist[u] + w) {
                dist[v] = dist[u] + w;
                past[v] = u;
                pq.push({dist[v], v});
            }
        }
    }

    for(int i = 0; i < n; i++) {
        if(i == s) continue;
        if(!flag[i]) {
            printf("%d -> %d (inf)\n", s, i);
            continue;
        }
        printf("%d -> %d (%d) ", s, i, dist[i]);
        vector<int> path;
        int cur = i;
        while(cur != s) {
            path.push_back(cur);
            cur = past[cur];
        }
        path.push_back(s);
        reverse(path.begin(), path.end());
        for(auto &e : path) {
            printf("%d ", e);
        }
        printf("\n");
    }

    return 0;
}