LearnToCP
Sign in
Navigation
HomeRoadmapProblemsAbout Us
Theory
Contest Knowledge
Selecting an IDEInteractive TasksOutput-Only Tasks
Basics
Your First ProgramData types and IOC++ syntaxModuloFunctionsVectorsMatricesTime Complexity
Sorting
SortingCounting sortRadix Sort
Optimization Techniques
Two PointersSum of numbers 1 to nPrefix sumBinary SearchGreedyBinary Search FunctionsBinary Search by AnswerDivide and Conquer
Binary Numbers
Binary NumbersNumbers in codeBitwise OperationsBitmasks
Math
Binary ExponentiationPrime NumbersPrime FactorizationGCD and LCMSieve of EratosthenesModified Sieve
Data Structures
StringsStackQueueMapsSetsPriority QueueCustom Criteria for FunctionsSegment TreesFenwick TreesSparse TablesUnion FindSqrt Decomposition
Combinatorics
Addition PrincipleMultiplication PrincipleCombinatoric ObjectsInclusion Exclusion Principle
Geometry
Geometry BasicsVectorsCross and Dot ProductLinesPolygonsAnglesPoint in PolygonDistances and Intersection PointsConvex HullCircles
Recursion
PointersRecursionGenerating Combinatoric Objects
Dynamic Programming
About DPDP problemsTree DPBitmask DP
Graph Theory
GraphsDFS and BFSShortest PathsTreesTopological SortingDijkstra's AlgorithmMinimum Spanning TreesShortest Path Algorithms
Advanced Graph Theory
BiconnectivityStrongly Connected ComponentsBipartite GraphGraph FlowAugmenting PathsFlow - Minimum Cut DualityHeavy-Light DecompositionCentroid Decomposition
Advanced Data Structures
2D and 3D Segment TreesLazy PropagationImplicit Segment TreesPersistent Segment TreesLowest Common AncestorTrieBalanced Binary Search TreesMo's Algorithm

Signal Delay

EasyProblem #68
Time LimitMemoryInputOutput
1 s64 MBstdinstdout

The channels are one-way. And the signal has arrived everywhere once the last computer has it.

A signal starts at one computer in a network and spreads through communication channels to every other computer it can reach, directly or through other computers. Each channel is one-way and takes a known amount of time to carry the signal across.

Every computer forwards the signal onward the moment it receives it, so a computer receives the signal along whichever route reaches it soonest. The network is done once the last computer has received it.

Write a program that finds how long it takes for the signal to reach every computer in the network.

Input

First line of input will be a single integer t - the number of testcases.
The first line of each testcase contains two integers n and m - the number of computers and the number of channels.
Each of the next m lines contains three integers u, v and w - a channel leading from computer u to computer v, which the signal needs w time to cross. It does not carry the signal from v to u.
The last line of the testcase contains a single integer s - the computer the signal starts from.

Computers are numbered 1 to n. There may be several channels between the same pair of computers, and a channel may lead from a computer back to itself.

Output

For every testcase print a single line with the time the signal needs to reach every computer, or −1 if some computer cannot receive it at all.

Example

Input
3
5 7
1 2 7
1 3 3
1 5 6
2 1 2
3 5 2
4 2 3
5 4 1
1
3 1
1 2 5
1
1 1
1 1 4
1
Output
7
-1
0

In the first testcase the signal reaches computer 3 in 3, and computer 5 in 5 - going 1→3→5 beats the direct channel, which costs 6. From there computer 4 is reached in 6, and computer 2 in 7 by the direct channel, since the route through 3,5,4 would cost 9. The last arrival is at time 7.

In the second testcase nothing leads to computer 3, so the answer is −1. In the third the signal starts at the only computer and is already there, so it takes no time at all.

Constraints

1≤t≤10
1≤n≤1000
1≤m≤105
1≤u,v≤n and 1≤s≤n
1≤w≤1000
The sum of n over all testcases does not exceed 5000, and the sum of m does not exceed 2⋅105


This problem was adapted, with permission, from Kašnjenje signala, authored by Društvo matematičara Srbije and Fondacija Petlja.

Submit your solution

Sign in to submit your solution and track your progress.

Sign in to submit