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
def main():
N, X, Y = map(int, sys.stdin.readline().split())
towns = []
for _ in range(N):
K = int(sys.stdin.readline())
sweets = []
for _ in range(K):
a, b, c = map(int, sys.stdin.readline().split())
sweets.append((a, b, c))
towns.append(sweets)
d = []
for _ in range(N):
row = list(map(int, sys.stdin.readline().split()))
d.append(row)
# Floyd-Warshall algorithm
for k in range(N):
for i in range(N):
for j in range(N):
if d[i][j] > d[i][k] + d[k][j]:
d[i][j] = d[i][k] + d[k][j]
# Precompute knapsack for each town
town_knapsack = []
for sweets in towns:
dp = [0] * (Y + 1)
for a, b, c in sweets:
remaining = c
k = 1
binary = []
while remaining > 0:
take = min(k, remaining)
binary.append((a * take, b * take))
remaining -= take
k *= 2
for cost, sat in binary:
for i in range(Y, cost - 1, -1):
if dp[i - cost] + sat > dp[i]:
dp[i] = dp[i - cost] + sat
optimized = []
max_sat = 0
for cost in range(Y + 1):
if dp[cost] > max_sat:
max_sat = dp[cost]
optimized.append((cost, max_sat))
town_knapsack.append(optimized)
max_answer = 0
from collections import deque
visited = {}
queue = deque()
initial = (0, 0, 0) # (current_town, y, x)
visited[initial] = 0
queue.append(initial)
# Check initial return to town 1
if d[0][0] <= X:
max_answer = 0
while queue:
u, y, x = queue.popleft()
current_sat = visited[(u, y, x)]
# Check return to town 1 from current state
cost_back = d[u][0]
total_x = x + cost_back
if total_x <= X and y <= Y and current_sat > max_answer:
max_answer = current_sat
for v in range(N):
move_cost = d[u][v]
new_x = x + move_cost
if new_x > X:
continue
for cost_v, sat_v in town_knapsack[v]:
new_y = y + cost_v
new_total_x = new_x + cost_v
if new_y > Y or new_total_x > X:
continue
new_sat = current_sat + sat_v
key = (v, new_y, new_total_x)
if key not in visited or new_sat > visited.get(key, -1):
visited[key] = new_sat
queue.append(key)
# Check return after buying
cost_back_v = d[v][0]
final_x = new_total_x + cost_back_v
if final_x <= X and new_y <= Y and new_sat > max_answer:
max_answer = new_sat
print(max_answer)
if __name__ == '__main__':
main()