Submission

Status:

-P--PP-P--

Subtask/Task Score:

40/100

Score: 40

User: Chayatoeyy

Problemset: Fast Delivery

Language: cpp

Time: 0.002 second

Submitted On: 2026-03-12 13:03:31

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n,edge;
    cin >> n >> edge;
    vector<pair<ll,ll>> vec[n];
    vector<ll> dist(n,1e9);
    vector<ll> bef(n,-1);
    for(int i=0;i<edge;i++){
        ll u,v,w;
        cin >> u >> v >> w;
        vec[u].push_back({v,w});
        vec[v].push_back({u,w});
    }
    priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> pq;
    int st;
    cin >> st;
    dist[st]=0;
    pq.push({0,st});
    while(!pq.empty()){
        ll u = pq.top().second;
        ll w = pq.top().first;
        pq.pop();
        if(w>dist[u]) continue;
        for(int i=0;i<vec[u].size();i++){
            ll v = vec[u][i].first;
            ll we = vec[u][i].second+w;
            if(we<dist[v]){
                dist[v] = we;
                bef[v] = u;
                pq.push({we,v});
            }
        }
    }
    for(int i=0;i<n;i++){
        if(i==st) continue;
        cout << st << " -> " << i << " (" << (dist[i]==1e9 ? "inf":to_string(dist[i])) ;
        cout << ") ";
        vector<ll> ans;
        int now = i;
        while(now!=-1){
            ans.push_back(now);
            now = bef[now];
        }
        for(int i = ans.size()-1;i>=0;i--){
            cout << ans[i] << " ";
        }
        cout << endl;
    }
}