Q . A representative of Richardson health department wants to investigate optima
ID: 3711726 • Letter: Q
Question
Q. A representative of Richardson health department wants to investigate optimal allocation strategy of ambulances in case of emergency. Write a program that can assign all demand points to a nearest dispatch station given the location information below.
irection Ll Align Text E Convert to SmartArt Arrange Quick ty Drawing Resource Allocation Q. A representative of Richardson health department wants to investigate optimal allocation strategy ambulances in case of emergency. Write a program that can assign all demand points to a nearest dispatch station given the location information below 10 20tt 17 25 2524419 Station Station 2 13 32 26 35 15 toExplanation / Answer
Here I have written a java code. In this we are taking 2 hashmaps with keys as stations and values as list of demand points. We calculate the distance of all the demand points from both stations one by one. Then we compare the distance and assign the demand points to the closer station.
Code
import java.util.Scanner;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
class GFG
{
// Function to calculate distance
static double distance(int x1, int y1, int x2, int y2)
{
// Calculating distance
return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2) * 1.0);
}
//Driver code
public static void main (String[] args)
{
Map<String, List<Integer>> map = new HashMap<String, List<Integer>>();
List<Integer> list1 = map.get("station 1");
List<Integer> list2 = map.get("station 2");
int sx1 = 8; //x coordinate of station 1
int sy1 = 16; //y coordinate of station 1
int sx2 = 35; //x coordinate of station 2
int sy2 = 13; //y coordinate of station 2
int[] x = new int[]{1,3,5,9,11,11,9,17,22,25,21,28,30,31,8,26,35};
int[] y = new int[]{5,30,10,24,22,20,4,25,30,19,5,32,31,1,35,2,15};
for(int i = 0; i< x.length ; i++){
double d1 = distance(sx1, sy1, x[i], y[i]);
double d2 = distance(sx2, sy2, x[i], y[i]);
if(d1 <= d2){
if (list1 == null)
{
list1 = new ArrayList<Integer>();
map.put("station 1", list1);
}
list1.add(i);
}else{
if (list2 == null)
{
list2 = new ArrayList<Integer>();
map.put("station 2", list2);
}
list2.add(i);
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.