Monk's Business Day
Practice
3.9 (29 votes)
Bellman ford algorithm
Graphs
Medium
Open
Shortest path algorithm
Problem
86% Success 3656 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

Monk visits Biksy, the largest trading market in the land. Biksy has traders from all over the world.
There are a total of N items indexed from 1 to N, that are traded in the market by a total of M dealers. Each trader is characterized by three integers, say i, j, C , meaning that the trader will take i'th item from you and give you j'th item and C units of money. A negative value of C signifies that, in order to get j'th item from the trader, you will have to give i'th item and C units of money. Note that there can be multiple dealers who deal with the same pair of items and some crazy dealers might trade the same item as well i.e. (i = j).
Monk visits Biksy having the item number 1. He collects the data of all the traders and wants to know if there is way by which he can become infinity rich if he acts smart! i.e. if there are a series of profits, repeating which, will always increase the number of units of money with him! Help Monk find the answer to this question. Note that Monk can go to any dealer any number of times.

Input:
First line contains an integer T. T test cases follow.
First line of each test case contains two space-separated integers N, M
Next M lines contain three space-separated integers i, j and C, the characteristics of the traders.

Output:
Print "Yes"(without the quotes) if such a way exists, "No"(without the quotes) otherwise.
Print the answer to each test case in a new line.

Constraints:
1 ≤ T ≤ 10
1 ≤ N ≤ 100
1 ≤ M ≤ 1000
1 ≤ i, jN
-1000 ≤ C ≤ 1000

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
11 votes
Tags:
ApprovedBasic ProgrammingGraphsMediumOpenShortest Path Algorithm
Points:30
3 votes
Tags:
AlgorithmsGraphsShortest Path Algorithms
Points:30
2 votes
Tags:
GraphsAlgorithmsShortest Path Algorithms