HihoCoder: Base Station Site Selection and hihocoder Site Selection

Source: Internet
Author: User

HihoCoder: Base Station Site Selection and hihocoder Site Selection

Time Limit: 256 ms single point time limit: Ms memory limit: MB
Description

A communication base station must be built on an N × M grid. The communication base station must only be built on a grid point.

There is A user in the grid. The communication cost for each user is the square Euclidean distance from the user to the base station.

There are also B communication companies in the grid. The price for maintaining the base station is the distance from the base station to the nearest communication company (the distance is defined as the distance from Manhattan ).

The total cost of building a base station in a grid is the sum of user communication costs plus the cost of maintaining the base station, and the minimum total cost.

Input

The first act is an integer T, indicating the number of data groups.

The first behavior of each data group is four integers: N, M, A, and B.

The next line A + B contains two integers x and y, representing A coordinate. The first line A represents the coordinates of each user, and the second line B represents the coordinates of each communication company.

Output

For each group of data, output a line "Case # X: Y", X indicates the data number (starting from 1), and Y indicates the minimum cost.

Data range

1 ≤ T ≤ 20

1 ≤ x ≤ N

1 ≤ y ≤ M

1 ≤ B ≤100

Small Data

1 ≤ N, M ≤ 100

1 ≤ A ≤ 100

Big Data

1 ≤ N, M ≤ 107

1 ≤ A ≤ 1000

Sample Input
23 3 4 11 22 12 33 22 24 4 4 21 22 43 14 31 41 3
Sample output
Case #1: 4Case #2: 13

[Resolution]

The intuitive idea is brute-force traversal, but this will inevitably time out. In fact, you do not need to traverse every grid, and the smallest sum of all users must be in addition to all user center locations, so you only need to judge ((X1 + x2 +... + xn)/n,(Y1 + y2 +... + yn)/n),((X1 + x2 +... + xn)/n + 1,(Y1 + y2 +... + yn)/n),((X1 + x2 +... + xn)/n,(Y1 + y2 +... + yn)/n + 1),((X1 + x2 +... + xn)/n + 1,(Y1 + y2 +... + yn)/n + 1) Which is the minimum distance between the four points and all users is OK. As for communication companies, as long as the nearest one is found, rather than the sum of the distance to all communication companies, and the distance to Manhattan is not the Euclidean distance, the impact is small.

[Java code]

import java.util.Scanner;public class Main {    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        int T = in.nextInt();                for (int t = 1; t <= T; t++) {        int N = in.nextInt();        int M = in.nextInt();        int A = in.nextInt();        int B = in.nextInt();                int[][] a = new int[A][2];        int[][] b = new int[B][2];                int rowSum = 0;        int colSum = 0;                for (int i = 0; i < A; i++) {        a[i][0] = in.nextInt();        a[i][1] = in.nextInt();        rowSum += a[i][0];        colSum += a[i][1];        }                for (int i = 0; i < B; i++) {        b[i][0] = in.nextInt();        b[i][1] = in.nextInt();        }                int minCost = Integer.MAX_VALUE;        int fromRow = rowSum / A;        int fromCol = colSum / A;        for (int i = fromRow; i <= fromRow + 1; i++) {        for (int j = fromCol; j <= fromCol + 1; j++) {        int cost = 0;        for (int k = 0; k < A; k++) {        cost += (i - a[k][0]) * (i - a[k][0]) + (j - a[k][1]) * (j - a[k][1]);        }                int costB = Integer.MAX_VALUE;        for (int k = 0; k < B; k++) {        int tmp = Math.abs(i - b[k][0]) + Math.abs(j - b[k][1]);        costB = Math.min(costB, tmp);        }                cost += costB;        minCost = Math.min(minCost, cost);        }        }                System.out.println("Case #" + t + ": " + minCost);        }    }}

Disclaimer: the subject is from hihoCoder.com. If there is any infringement, contact the blogger to delete the post.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.