一、问题
新便利店要在某一个小区选址了,需要辐射更多的小区用户,有n个小区(x、y坐标),每个小区的住户数为该小区的权重值。请帮便利店选一个带权距离之和(整形)最小的地址。(全程int数据,没有用到浮点型数据)
二、要求
输入格式:
第一行为单个整数n,表示小区的个数。
以下n行为3个整数,分别为x,y,num住户数。
输出格式:
第一个数为便利店选址的小区编号,中间空一格,第二个数为便利店与各小区住户数的带权路径和。
输入样例:
在这里给出一组输入。例如:
3
2 2 30
1 2 10
3 2 5
输出样例:
在这里给出相应的输出。例如:
1 15
三、代码
import java.util.ArrayList;
import java.util.Scanner;
public class Main{
static class Node{
int x;
int y;
int weight;
public Node(int x,int y,int weight){
this.x=x;
this.y=y;
this.weight=weight;
}
}
public static void main(String[] args) {
int min=-1;
int dist=999999999;
Scanner scanner=new Scanner(System.in);
ArrayList<Node> list=new ArrayList();
String n1=scanner.nextLine();
int n=Integer.parseInt(n1);
for (int i = 0; i<n ; i++) {
String s= scanner.nextLine();
String ss[]=s.split(" ");
Node node=new Node(Integer.parseInt(ss[0]),Integer.parseInt(ss[1]),Integer.parseInt(ss[2]));
list.add(node);
}
for (int i = 0; i < n; i++) {
int temp=0;
int x1= list.get(i).x;
int y1=list.get(i).y;
for (int j = 0; j <list.size(); j++) {
int x2= list.get(j).x;
int y2=list.get(j).y;
double temp1= Math.sqrt(Math.pow((x1-x2),2)+Math.pow((y1-y2),2))*list.get(j).weight;
temp=temp+ (int) temp1;
}
if(temp<=dist){
dist=temp;
min=i+1;
}
}
System.out.print(min+" "+dist);
}
}