Special paths
Practice
4.3 (7 votes)
Binary search
Algorithms
C++
Problem
83% Success 1271 Attempts 50 Points 1s Time Limit 256MB Memory 1024 KB Max Code
You are given an undirected connected graph \(G\) with \(N\) nodes and \(M\) edges. Every node has a value \(A[i]\) assigned to it.
The value of a simple path between node \(u\) and \(v\) is as follows:
- The maximum absolute difference between the values of adjacent nodes present in the simple path.
Find the minimum possible path value of any simple paths between start and end nodes.
Input format
- The first line contains two space-separated integers \(N\) and \(M\) denoting the number of nodes and edges respectively.
- Next \(M\) lines contain two space-separated integers denoting edges.
- The next line contains \(N\) space-separated integers denoting node values.
- The next line contains two space-separated integers denoting the start ends.
Output format
Print the minimum possible path value of any simple path between start and end nodes.
Constraints
\(1 \le N \le 10^5\)
\(N - 1 \le M \le \text{min} (10^5 , N \times (N-1)/2)\)
\(1 \le A[i] \le 10^6\)
Explanation
There are 3 simple paths between nodes 2 and 4 :-
- 2 - 3 - 4 : Value is max(abs(A[2] - A[3]), abs(A[3] - A[4])) = max(1,5) = 5.
- 2 - 4 : Value is abs(A[2] - A[4]) = 4
- 2 - 1 - 4 : Value is 7.
Code Editor
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
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
1.Robots
Points:30
246 votes
Tags:
ReadyMathematicsSortingApprovedEasy-Medium
Points:50
5 votes
Tags:
Binary SearchTreesMathAlgorithms
Points:50
4 votes
Tags:
AlgorithmsBinary Search
Editorial
Login to unlock the editorial
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
Results
Custom Input
Run your code to see the output