Submission

Status:

PPPPPPPPPP

Subtask/Task Score:

100/100

Score: 100

User: colo39

Problemset: Fast Delivery

Language: cpp

Time: 0.004 second

Submitted On: 2026-11-28 13:59:20

#include <bits/stdc++.h>
using namespace std;
vector<pair<int,int>> vec[100005];
int dist[100005], par[100005];
int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n,m;
    cin >> n >> m;
    for (int i =1;i <= m;i++) {
        int u,v,w;
        cin >> u >> v >> w;
        vec[u].push_back({v,w});
        //vec[v].push_back({u,w});
    }
    for (int i = 0;i < n;i++) dist[i] = 1e9;
    int st;
    cin >> st;
    priority_queue<pair<int,int>, vector<pair<int,int>>, greater<>> pq;
    pq.push({0,st});
    dist[st] = 0;
    par[st] = -1;
    while (!pq.empty()) {
        auto u = pq.top();
        
        pq.pop();
        if (dist[u.second] < u.first) continue;
        
        for (auto &x : vec[u.second]) {
            pair<int,int> p = {u.first+x.second, x.first};
            //cout << p.first << " " << dist[p.second] << endl;
            if (p.first >= dist[p.second]) continue;
            
            dist[p.second] = p.first;
            par[p.second] = u.second;
            
            pq.push(p);
        }
    }
    for (int i = 0;i < n;i++) {
        if (i == st) continue;
        cout << st << " -> " << i;
        if (dist[i] == 1e9) {
            cout << " (inf) \n";
            continue;
        }
        else cout << " (" << dist[i] << ") ";
        int hold = i;
        vector<int> temp;
        for (;hold != -1;hold = par[hold]) {
            temp.push_back(hold);
        }
        //temp.push_back(st);
        reverse(temp.begin(),temp.end());
        for (auto &x : temp) cout << x << " ";
        cout << endl;
    }
    return 0;
}