#include #include #include #include #include using node = std::pair; int main() { int n, m, x, y; scanf("%d %d %d %d", &n, &m, &x, &y); int *first = new int[n + 1]{}; int *to = new int[m * 2 + 1]; int *next = new int[m * 2 + 1]; int *weight = new int[m * 2 + 1]; int *visited = new int[n + 1]{}; int *dist = new int[n + 1]; int edge = 1; int a, b, c; for (int i = 0; i != m; ++i) { scanf("%d %d %d", &a, &b, &c); to[edge] = b; weight[edge] = c; next[edge] = first[a]; first[a] = edge; ++edge; to[edge] = a; weight[edge] = c; next[edge] = first[b]; first[b] = edge; ++edge; } memset(dist, -1, sizeof(*dist) * (n + 1)); std::priority_queue, std::greater> pq; int current, to_node; dist[x] = 0; pq.push(node{ 0, x }); while (!pq.empty()) { current = pq.top().second; pq.pop(); if (current == y) break; if (visited[current]) continue; visited[current] = 1; edge = first[current]; while (edge) { to_node = to[edge]; if (dist[to_node] == -1 || dist[to_node] > dist[current] + weight[edge]) { dist[to_node] = dist[current] + weight[edge]; pq.push(node{ dist[to_node], to_node }); } edge = next[edge]; } } printf("%d\n", dist[y]); }