#include <climits>
#include <functional>
#include <queue>
#include <vector>
using namespace std;
#define ll long long

struct Edge {
  int y;
  ll weight;
};

struct InQueue {
  int y;
  ll l;

  bool operator<(const InQueue &rhs) const { return l < rhs.l; }
  bool operator>(const InQueue &rhs) const { return l > rhs.l; }
};

void mincammino(int N, int M, vector<int> X, vector<int> Y, vector<int> P,
                vector<long long> &D) {
  D[0] = 0;
  for (int i = 1; i < N; i++) {
    D[i] = LLONG_MAX;
  }
  vector<vector<Edge>> edges(N);
  vector<bool> visited(N, false);
  for (int i = 0; i < M; i++) {
    edges[X[i]].push_back({Y[i], P[i]});
  }

  priority_queue<InQueue, vector<InQueue>, less<InQueue>> q{};
  q.push({0, 0});

  while (!q.empty()) {
    auto top = q.top();
    q.pop();
    if (visited[top.y]) {
      continue;
    }
    visited[top.y] = true;
    for (Edge e : edges[top.y]) {
      if (e.weight + top.l < D[e.y]) {
        D[e.y] = e.weight + top.l;
        q.push({e.y, e.weight + top.l});
      }
    }
  }

  for (int i = 0; i < N; i++) {
    if (D[i] == LLONG_MAX) {
      D[i] = -1;
    }
  }
}
