#include <bits/stdc++.h>
using namespace std;
using ll = long long;

void mincammino(int N, int M, vector<int> X, vector<int> Y, vector<int> P, vector<long long>& D) {
    fill(D.begin(), D.end(), -1);
    vector<vector<pair<ll, ll>>> nodes(N); // 1: arrivo, 2: costo
    for (ll i{ }; i < M; ++i) nodes[X[i]].push_back({ Y[i], P[i] });

    unordered_map<ll, vector<int>> t2n; // Nodes for each ending time
    set<ll> fired = { 0 }; // tempo fine
    t2n[0].push_back(0);
    ll time = { };
    D[0] = 0;
    while (fired.size() > 0) {
        for (int& f : t2n[*fired.begin()]) {
            ll end_t{ *fired.begin() };
            time = end_t;
            for (const pair<ll, ll>& conn : nodes[f]) {
                ll n2{ conn.first }, weight{ conn.second };
                if (D[n2] == -1 || D[n2] > D[f] + weight) {
                    D[n2] = time + weight;
                    fired.insert(D[n2]);
                    t2n[D[n2]].push_back(n2);
                }
            }
        }
        fired.erase(fired.begin());
    }
}
