House Travelling Problem
Practice
5 (1 votes)
Recruit
Hiring
Minimum spanning tree
Algorithms
Easy Medium
Greedy algorithm
Ready
Approved
Problem
51% Success 813 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

Bob is about to visit N of his friends' houses. Each house is depicted as a point \((x_i , y_i)\) on the \(X-Y\) plane. The length of roads between two houses i and j is defined as \(F(i, j) = (x_i - x_j)^2 + (y_i - y_j)^2\).

There's a \(N \times N\) matrix M filled with 1s and 0s. \(M[i][j] = 1\) denotes that it is possible to travel to \(i^{th}\) House from \(j^{th}\) House and vice versa. If \(M[i][j] = 0\), it is not possible to travel to \(i^{th}\) House from \(j^{th}\) House and vice versa.

Now, Bob wants to visit all the houses, but the sum of length of all roads traveled should be maximum. There's one more restriction, he can use a maximum of \((N - 1)\) roads. Before starting the trip, he would like to know what is the maximum distance he can travel which satisfies all the given conditions. If it is not possible to visit all the houses, print out -1.

Input Format:
The first line contains N, the number of houses Bob wants to visit.
N lines follow, each contains two space separated integers \(x_i\) and \(y_i\) which denotes the of the \(i^{th}\) house on the \(X-Y\) plane.
Again, N lines follow, each contains N space separated integers, each being 0 or 1, denoting the matrix M.

Output Format:
Print the required answer in one line.

Input Constraints:
\(1 \le N \le 3000 \)
\(-10^6 \le x_i, y_i \le 10^6\)

Note:
One road can be traversed any number of times , but its contribution would be taken only once. Also, there's large input data, please use faster i/o methods.

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:20
17 votes
Tags:
MathematicsOpenApprovedEasyMathamatics
Points:50
Tags:
Dynamic ProgrammingLinear AlgebraHardApprovedMathematicsOpenMatrix Exponentiation
Points:30
7 votes
Tags:
BitmaskAlgorithmsEasy-MediumReadyApprovedBit manipulation