## Algorithm

The Mayor of a city wants to improve the attentions of the local government to the citizens. For that, five offices to attend to consults will be open in the city. The Mayor thinks that the attentions would be better if the offices are close to where the people live. To decide where to open the offices, the city has been divided in 25 areas, in a square: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 and the quantity of people living in each square is counted, thus assigning to each area the number (an integer number) of thousands of people living there. For example: 0 0 2 3 1 5 2 2 2 0 1 1 2 3 0 0 0 2 1 5 2 1 2 0 4 The number of people in each area is less or equal to 10 million. For each area, the minimum distance to the five offices is obtained as follows: the distance from one area to an office is obtained as the number of movements to go from the area to the area where the office will be installed (the movements are in horizontal and vertical; for example, from (1,1) to (2,3) three movements are necessary). And we want to minimize the sum of the minimum distances from all the areas, but having into account the quantity of people living in each area. Thus, for each area the distance to an office is multiplied by the value associated to the area. Input The input begins with a line where the number of test cases (t) is indicated. The data for each test case appear in sucessive lines. In each test case the first line contains the number of areas in the city with non null population (n) and in each one of the n following lines three numbers appear, the first and the second indicate the row and the column of the area (they are numbered from 0 to 4) and the third the number of thousand of people living in the area. Output The output consists of a line for each problem, with five numbers with a space between each two numbers, and no space after the last number. The numbers in the solutions are written in increasing order. If there is more than one optimum solution, you must output the solution with lowest first value. If the first value coincides, you must output the solution with lowest second value, and so on.

Sample Input 4 1 2 2 1 4 0 0 1 4 4 1 0 4 1 4 0 1 5 0 0 1 1 1 1 2 2 1 3 3 1 4 4 1 7 4 2 2 3 3 1 2 4 3 2 1 1 1 3 4 1 2 2 1 0 1

Sample Output 0 1 2 3 12 0 1 4 20 24 0 6 12 18 24 5 7 8 14 22

## Code Examples

### #1 Code Example with C Programming

Code - C Programming

#include <bits/stdc++.h>

using namespace std;

int main()
{
int t,n,v;
int x,y,s;
cin >> t;
while(t--){
cin >> n;
vector < tuple<int,int,int>> pops;
for(int i=0;i < n;i++){
cin >> x >> y >> s;
pops.push_back(make_tuple(x,y,s));
}
int best = INT_MAX;
vector<int> bestRes;
// for each office combination, compute cost
for(int i=0;i < 25;i++){
for(int j=i+1;j < 25;j++){
for(int k=j+1;k < 25;k++){
for(int l=k+1;l < 25;l++){
for(int m=l+1;m < 25;m++){
int cost = 0;
for(auto& t : pops){
int minCost = INT_MAX;
for(int office : {i,j,k,l,m}){
minCost = min(abs(office/5-get < 0>(t))+abs(office%5-get<1>(t)),minCost);
}
cost += (minCost*get<2>(t));
}
if(cost < best){
best = cost;
bestRes = {i,j,k,l,m};
}
}
}
}
}
}
printf("%d %d %d %d %d\n",bestRes[0],bestRes[1],bestRes[2],bestRes[3],bestRes[4]>;
}
}

Copy The Code &

Input

cmd
4
1
2 2 1
4
0 0 1
4 4 1
0 4 1
4 0 1
5
0 0 1
1 1 1
2 2 1
3 3 1
4 4 1
7
4 2 2
3 3 1
2 4 3
2 1 1
1 3 4
1 2 2
1 0 1

Output

cmd
0 1 2 3 12
0 1 4 20 24
0 6 12 18 24
5 7 8 14 22