Graph Deletion
You are given a directed weighted graph represented by an adjacency matrix matrix. The value matrix[i][j] represents the weight of the directed edge from vertex i to vertex j.
You are also given an integer array queries which represents a sequence of vertex deletions. When a vertex is deleted, all directed edges going into and out of that vertex are also permanently removed.
Before each vertex is deleted, you must calculate the sum of the shortest paths between all pairs of currently remaining vertices.
Return an array of integers representing the sum of shortest paths before each deletion step.
Example 1:
Input: matrix = [[0, 5], [4, 0]], queries = [0, 1]
Output: [9, 0]
Explanation:
Before deleting node 0: Both nodes 0 and 1 exist. Shortest path 0->1 is 5, 1->0 is 4. Sum = 9.
Delete node 0. Before deleting node 1: Only node 1 exists. No pairs available. Sum = 0.
Example 2:
Input: matrix = [[0]], queries = [0]
Output: [0]
Explanation:
Only one node exists, so there are no paths between distinct pairs.
Now Your Turn!
Pick the correct output for the given inputInput: matrix = [[0, 10, 5], [2, 0, 3], [3, 1, 0]], queries = [1, 2, 0]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= matrix.length <= 500
- matrix.length == matrix[i].length
- queries.length == matrix.length
- 0 <= queries[i] < matrix.length
- queries is a valid permutation of all vertex indices from 0 to matrix.length - 1.
- 0 <= matrix[i][j] <= 105
- matrix[i][i] == 0