Path Value
Practice
3.5 (4 votes)
Graph representation
Algorithms
Graphs
C++
Problem
87% Success 2904 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code
Given a connected graph \(G\) with \(N\) nodes and \(M\) edges (edges are bi-directional). Every node is assigned a value \(A[i]\). We define a value of a simple path as :
- Value of path = Maximum of (absolute difference between values of adjacent nodes in a path).
A path consists of a sequence of nodes starting with start node \(S\) and end node \(E\).
- \(S - u_1 - u_2 - .... - E\) is a simple path if all nodes on the path are distinct and \(S, u_1, u_2, ..., E\) are nodes in \(G\).
Given a start node \(S\) and end node \(E\), find the minimum possible "value of path" which starts with node \(S\) and ends with node \(E\).
Input format
- First line contains two space-separated integers \(N \ M\)
- Second line contains two space-separated integers \(S \ E\)
- Next M lines contain two space-separated integers u v, denoting an edge between node u and v
- Next line contains \(N\) space-separated integers denoting values of nodes.
Output format
Print a single integer — minimum possible "value of path" between node \(S\) and \(E\).
Constraints
\(1 \le N \le 10^5 \\ N - 1 \le M \le 10^6 \\ \\ 1 \leq S, E \leq N \\ S \neq E \\ \\ 1 \leq u, v \leq N \\ 1 \le A[i] \le 10^6\)
Submissions
Please login to view your submissions
Similar Problems
Points:30
72 votes
Tags:
Medium
Points:30
2 votes
Tags:
C++AlgorithmsGraphsGraph Representation
Points:20
4 votes
Tags:
MathematicsOpenApprovedEasyMathamatics
Editorial