Development Cost
Practice
3.2 (8 votes)
Algorithms
Binary search
Depth first search
Disjoint set
Easy
Graphs
Life Cycle assessment
Searching
Problem
67% Success 601 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

A country is represented as a matrix of the size \(N \times M\) and each cell is considered to be a city. 

There are \(Q\) missions and you are required to accomplish \(K\) missions to receive the tourist certification. In each mission, you are given a source and destination and you need to travel from source to destination.
Each city contains a development cost that depicts the state of development of the city. The development cost of a mission is considered to be the maximum development cost in the path between source and destination.The overall development cost for the certification is the maximum development cost of all the accomplished missions. Your task is to minimize the overall development cost to achieve the certificate. 

Note: 

  • A path can visit a particular cell multiple times. It is possible to go up, down, left, and right from any cell. 
  • It is not necessary to accomplish the first \(K\) missions. You can accomplish any \(K\) missions out of the given \(Q\) missions.
  • It is not necessary to travel from a source to a destination by using the shortest path. You are allowed to take any path to travel from a source to a destination. 

Input format

  • First line: Four integers \(N\), \(M\), \(Q\), and \(K\) respectively
  • Next \(N\) lines: \(M\) integers, where the \(j^{th}\) integer on the \(i^{th}\) line representing \(A_{ij}\)
  • Next \(Q\) lines: Four integers \(X_{source}, Y_{source}, X_{destination},\ and\ Y_{destination}\) respectively

Note: It is guaranteed that the source city and destination city do not coincide for a mission.

Output format

Print the minimum value of the overall development cost in accomplishing exactly \(K\) missions.

Input Constraints

\(1 \le N, M \le 500\)

\(1 \le K \le Q \le 5 \times 10^5\)

\(1 \le X_{source}, X_{destination} \le N\)

\(1 \le Y_{source}, Y_{destination} \le M\)

\(1 \le A_{ij} \le 10^9\)

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
5 votes
Tags:
Binary SearchAlgorithms
Points:30
11 votes
Tags:
AlgorithmsBinary SearchEasySearching
Points:30
81 votes
Tags:
AlgorithmsBinary SearchEasyHash MapsSearchingSortingString Manipulation