Shooting Gallery (Codeforces 30C)
Tags: DP, probabilities

One warm and sunny day king Copa decided to visit the shooting gallery, located at the Central Park, and try to win the main prize — big pink plush panda. The king is not good at shooting, so he invited you to help him.The shooting gallery is an infinite vertical plane with Cartesian coordinate system on it. The targets are points on this plane. Each target is described by it's coordinates xi, and yi, by the time of it's appearance ti and by the number pi, which gives the probability that Copa hits this target if he aims at it.A target appears and disappears instantly, so Copa can hit the target only if at the moment ti his gun sight aimed at (xi, yi). Speed of movement of the gun sight on the plane is equal to 1. Copa knows all the information about the targets beforehand (remember, he is a king!). He wants to play in the optimal way, which maximizes the expected value of the amount of hit targets. He can aim at any target at the moment 0.

Input: The first line contains integer n (1 ≤ n ≤ 1000) — amount of targets in the shooting gallery. Then n lines follow, each describing one target. Each description consists of four numbers xi, yi, ti, pi (where xi, yi, ti — integers,  - 1000 ≤ xi, yi ≤ 1000, 0 ≤ ti ≤ 109, real number pi is given with no more than 6 digits after the decimal point, 0 ≤ pi ≤ 1). No two targets may be at the same point.

Output: Output the maximum expected value of the amount of targets that was shot by the king. Your answer will be accepted if it differs from the correct answer by not more than 10 - 6.


Sample Inputs:
1
0 0 0 0.5

2
0 0 0 0.6
5 0 5 0.7

Sample Outputs:
0.5000000000

1.3000000000

Solution (Python):
import heapq

def is_reachable(from_state, target):
    return (from_state[0] - target[0]) ** 2 + (from_state[1] - target[1]) ** 2 <= (target[2] - from_state[2]) ** 2
num_iter = int(input())
targets = sorted([list(map(float, input().strip().split(' '))) for dummy in range(0, num_iter)], key=lambda single_target: single_target[2])
states = []
for (i, target) in enumerate(targets):
    score_estimate = target[3]
    sorted_states = heapq.nlargest(len(states), states)
    for j in range(len(sorted_states)):
        if is_reachable(sorted_states[j][1], target):
            score_estimate += sorted_states[j][1][3]
            break
    target[3] = score_estimate
    heapq.heappush(states, (score_estimate, target))
max_score = heapq.nlargest(1, states)[0][1][3]
print(max_score)

Solution (Python):
n = int(input())
ts = sorted((list(map(float, input().strip().split())) for _ in range(n)), key=lambda x: x[2])
d = [0] * n
ans = 0
for i in range(n):
    d[i] = ts[i][3]
    for j in range(i):
        if (ts[i][0] - ts[j][0]) ** 2 + (ts[i][1] - ts[j][1]) ** 2 <= (ts[i][2] - ts[j][2]) ** 2:
            d[i] = max(d[i], d[j] + ts[i][3])
    ans = max(ans, d[i])
print('%.9f' % ans)

Solution (Python):
n = int(input())
ts = sorted((list(map(float, input().strip().split())) for _ in range(n)), key=lambda x: x[2])
d = [0] * n
ans = 0
for i in range(n):
    d[i] = ts[i][3]
    for j in range(i):
        if (ts[i][0] - ts[j][0]) ** 2 + (ts[i][1] - ts[j][1]) ** 2 <= (ts[i][2] - ts[j][2]) ** 2:
            d[i] = max(d[i], d[j] + ts[i][3])
    ans = max(ans, d[i])
print('%.9f' % ans)