Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- from io import StringIO
- from scipy.spatial import distance_matrix
- input = """8 3 3
- 3 -2 10
- -1 1 15
- -1 4 10
- 3 2 20
- 4 3 20
- -3 -4 25
- 2 -3 15
- 0 2 10
- """
- # Parse Input:
- f_inp = np.loadtxt(StringIO(input))
- n, p, d = f_inp[0]
- n = int(n) # number of cities should be integer
- p = int(p) # number of choosed cities should be integer
- xy = f_inp[1:, 0:2]
- pop = f_inp[1:, 2]
- # Greedy Algorithm
- choice_lst = []
- sum_pop = 0
- distance = distance_matrix(xy, xy)
- for i in range(p):
- weighted_pop = (distance <= d)*pop
- weighted_sum = weighted_pop.sum(axis=1)
- choice = np.argmax(weighted_sum) # argmax gives the best choice: Greedy
- covered = np.flatnonzero(weighted_pop[choice]) # cities in that choice
- pop[covered] = 0 # Zeros the city already covered
- sum_pop += weighted_sum[choice] # Total num pop
- choice_lst.append(choice+1)
- # Output
- choice_str = " ".join([str(x) for x in choice_lst])
- print("%s %d"%(choice_str,sum_pop))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement