Only Premium Users can view the Question
Problem Statement:
A logistics company has a network of distribution centers arranged in a grid withnrows andmcolumns. Each cell in this grid contains a distribution center identified by its center ID, represented as an integer from 1 to 100,000.
The company wants to analyze the travel cost between distribution centers of the same ID. The travel cost between two centers located at (r1, c1) and (r2, c2) is defined by the Manhattan distance: the shortest path moving horizontally or vertically between them. For example, in a 3x4 grid, the Manhattan distance between (1, 2) and (3, 3) is 3, and one possible path is: (1, 2) (2, 2) (2, 3) (3, 3).
The task is to calculate the sum of the Manhattan distances between each pair of distribution centers with the same ID, for every ID in the grid.
Input Format:
Background:
A water supply company has constructed a network of pipelines to distribute water from a central reservoir to all connected towns. The network is organized in a balanced tree structure, with the reservoir as the root. Each pipeline has a maintenance cost based on its length and condition.
Objective:
You need to manage this pipeline network by calculating the water transport cost between towns and updating pipeline maintenance costs as needed.
Functionality Requirements:
Input Format:
Output Format: