Tomorrow is finally the day of the excursion to Maizu Elementary School. Gatcho, who attends Maizu Elementary School, noticed that he had forgotten to buy tomorrow's sweets because he was so excited. Gaccho wants to stick to sweets so that he can enjoy the excursion to the fullest.
Gaccho gets a X yen allowance from his mother and goes to buy sweets. However, according to the rules of elementary school, the total amount of sweets to bring for an excursion is limited to Y yen, and if it exceeds Y yen, all sweets will be taken up by the teacher.
There are N towns in the school district where Gaccho lives, and each town has one candy store. Each town is assigned a number from 1 to N, and Gaccho lives in town 1. Each candy store sells several sweets, and the price per one and the satisfaction level that you can get for each purchase are fixed. However, the number of sweets in stock is limited. Also, Gaccho uses public transportation to move between the two towns, so to move directly from town to town , you only have to pay yen. It takes.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
At first, Gaccho is in Town 1. Gaccho should make sure that the sum of the total cost of moving and the price of the sweets you bought is within X yen, and the total price of the sweets you bought is within Y yen. I'm going to buy sweets. You must have arrived at Town 1 at the end. Gaccho wants to maximize the total satisfaction of the sweets he bought. Find the total satisfaction when you do the best thing.
Constraints
The
input satisfies the following conditions.
1leqNleq14
1leqXleq10000
1leqYleqmin(1000,X)
1leqKleq300
1leqaileq1000
1leqbileq1000
1leqcileq1000
0leqdi,jleq10000
di,i=0
Input
All inputs are given as integers in the following format:
NXY
Information on candy stores in town 1
Information on candy stores in town 2
...
Information on candy stores in town Nd1,1d1,2 ... d1,Nd2,1d2,2 ... d2,N
...
dN,1dN,2 ... dN,N
On the first line, the number of towns N, the amount of money you have X, and the maximum amount you can use to buy sweets Y are given, separated by blanks.
From the second line, information on each N candy store is given.
In the following N row and N column, the amount di,j required to go back and forth between town i and town j is given, separated by blanks.
Information on candy stores in each town is given in the following format.
Ka1b1c1a2b2c2
...
aKbKcK
The first line gives K, the number of types of sweets sold at the candy store. In the following K line, the price of one candy ai, the satisfaction level bi per candy, and the number of stocks ci are given, separated by blanks.
Output
Output the maximum value of the total satisfaction level on one line.
import sys
from collections import defaultdict
def main():
N, X, Y = map(int, sys.stdin.readline().split())
candy_stores = []
for _ in range(N):
K = int(sys.stdin.readline())
store = []
for _ in range(K):
a, b, c = map(int, sys.stdin.readline().split())
store.append((a, b, c))
candy_stores.append(store)
# Read distance matrix
dist = []
for _ in range(N):
row = list(map(int, sys.stdin.readline().split()))
dist.append(row.copy())
# Floyd-Warshall to compute minimal distance
for k in range(N):
for i in range(N):
for j in range(N):
if dist[i][j] > dist[i][k] + dist[k][j]:
dist[i][j] = dist[i][k] + dist[k][j]
# Precompute knapsack for each town
knapsack = []
for store in candy_stores:
items = []
for a, b, c in store:
remaining = c
k = 1
while remaining > 0:
take = min(k, remaining)
items.append((a * take, b * take))
remaining -= take
k *= 2
dp = [-1] * (Y + 1)
dp[0] = 0
for a, b in items:
for y in range(Y, a - 1, -1):
if dp[y - a] != -1:
dp[y] = max(dp[y], dp[y - a] + b)
knapsack.append(dp)
# Initialize DP states
dp_states = defaultdict(list)
dp_states[(0, 0)].append((0, 0)) # (current_town, y_used) -> list of (x_used, sat)
# Process all states
processed = set()
queue = [(0, 0)]
while queue:
current_town, y_used = queue.pop(0)
if (current_town, y_used) in processed:
continue
processed.add((current_town, y_used))
entries = dp_states[(current_town, y_used)]
for x_used, sat in entries:
for next_town in range(N):
movement_cost = dist[current_town][next_town]
new_x_move = x_used + movement_cost
if new_x_move > X:
continue
town_knapsack = knapsack[next_town]
max_y_inc = Y - y_used
for y_inc in range(0, max_y_inc + 1):
if y_inc > 0 and (town_knapsack[y_inc] == -1):
continue
sat_inc = town_knapsack[y_inc] if y_inc > 0 else 0
new_y = y_used + y_inc
new_x = new_x_move + y_inc
if new_x > X or new_y > Y:
continue
new_sat = sat + sat_inc
key = (next_town, new_y)
new_entry = (new_x, new_sat)
entries_new = dp_states[key]
add = True
to_remove = []
for idx, (x, s) in enumerate(entries_new):
if x <= new_entry[0] and s >= new_entry[1]:
add = False
break
if x >= new_entry[0] and s <= new_entry[1]:
to_remove.append(idx)
if add:
for idx in reversed(to_remove):
del entries_new[idx]
entries_new.append(new_entry)
dp_states[key] = entries_new
if key not in queue and key not in processed:
queue.append(key)
# Check all possible states and return to town 0 (original town 1)
max_sat = 0
for (current_town, y_used), entries in dp_states.items():
for x_used, sat in entries:
movement_cost = dist[current_town][0]
final_x = x_used + movement_cost
if final_x <= X and sat > max_sat:
max_sat = sat
print(max_sat)
if __name__ == "__main__":
main()