#include #include #include #include using std::vector; using std::priority_queue; int distance(vector > &adj, vector > &cost, int s, int t) { vector dists(adj.size(), std::numeric_limits::max()); std::priority_queue, std::greater> pq; dists[s] = 0; pq.push(s); while(!pq.empty()) { int n = pq.top(); int dist = dists[n]; /* std::cout << " pop: " << n + 1 << " : " << dist << std::endl; */ pq.pop(); for (int i = 0; i < adj[n].size(); i++) { int a = adj[n][i]; int nd = dist + cost[n][i]; if (dists[a] > nd) { dists[a] = nd; /* std::cout << " push: " << a + 1 << " : " << dists[a].first << std::endl; */ pq.push(a); } } } return (dists[t] == std::numeric_limits::max() ? -1 : dists[t]); } int main() { int n, m; std::cin >> n >> m; vector > adj(n, vector()); vector > cost(n, vector()); for (int i = 0; i < m; i++) { int x, y, w; std::cin >> x >> y >> w; adj[x - 1].push_back(y - 1); cost[x - 1].push_back(w); } int s, t; std::cin >> s >> t; s--, t--; std::cout << distance(adj, cost, s, t) << '\n'; }