GUPTA MECHANICAL

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

Sunday, 4 September 2022

[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 Ui and Vi with a length of Wi. By using the i-th edge, something can move from Ui to Vi, but not from Vi to Ui.

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 Ui to vertex Vi, Pak Chanek needs a time of Wi 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 2pN, 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 (2N1050M2105) — the number of vertices and edges in the graph.

Solution Click Below:-  ðŸ‘‰CLICK HERE👈
👇👇👇👇👇

The i-th of the next M lines contains three integers UiVi, and Wi (1Ui,ViNUiVi1Wi109) — a directed edge that connects two different vertices Ui and Vi with a length of Wi. There is no pair of different edges i and j such that Ui=Uj and Vi=Vj.

Output

Output a line containing N1 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.

No comments:

Post a Comment