GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

[Solution] Moving Both Hands Codeforces Solution

M. Moving Both Hands
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Pak Chanek is playing one of his favourite board games. In the game, there is a directed graph with $N$ vertices and $M$ edges. In the graph, edge $i$ connects two different vertices ${U}_{i}$ and ${V}_{i}$ with a length of ${W}_{i}$. By using the $i$-th edge, something can move from ${U}_{i}$ to ${V}_{i}$, but not from ${V}_{i}$ to ${U}_{i}$.

To play this game, initially Pak Chanek must place both of his hands onto two different vertices. In one move, he can move one of his hands to another vertex using an edge. To move a hand from vertex ${U}_{i}$ to vertex ${V}_{i}$, Pak Chanek needs a time of ${W}_{i}$ seconds. Note that Pak Chanek can only move one hand at a time. This game ends when both of Pak Chanek's hands are on the same vertex.

Pak Chanek has several questions. For each $p$ satisfying $2\le p\le N$, you need to find the minimum time in seconds needed for Pak Chanek to end the game if initially Pak Chanek's left hand and right hand are placed on vertex $1$ and vertex $p$, or report if it is impossible.

Input

The first line contains two integers $N$ and $M$ ($2\le N\le {10}^{5}$$0\le M\le 2\cdot {10}^{5}$) — the number of vertices and edges in the graph.

Solution Click Below:-  👉
👇👇👇👇👇

The $i$-th of the next $M$ lines contains three integers ${U}_{i}$${V}_{i}$, and ${W}_{i}$ ($1\le {U}_{i},{V}_{i}\le N$${U}_{i}\ne {V}_{i}$$1\le {W}_{i}\le {10}^{9}$) — a directed edge that connects two different vertices ${U}_{i}$ and ${V}_{i}$ with a length of ${W}_{i}$. There is no pair of different edges $i$ and $j$ such that ${U}_{i}={U}_{j}$ and ${V}_{i}={V}_{j}$.

Output

Output a line containing $N-1$ integers. The $j$-th integer represents the minimum time in seconds needed by Pak Chanek to end the game if initially Pak Chanek's left hand and right hand are placed on vertex $1$ and vertex $j+1$, or $-1$ if it is impossible.