Special paths

4.3

7 votes
Binary Search, Algorithms, C++
Problem

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

1N105N1Mmin(105,N×(N1)/2)1A[i]106

Sample Input
4 5
1 2
2 3
3 4
4 1
2 4
8 5 6 1
2 4
Sample Output
4
Time Limit: 1
Memory Limit: 256
Source Limit:
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.
Editor Image

?