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
python
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 functools import cmp_to_key
from collections import deque
def main():
N, X, Y = map(int, sys.stdin.readline().split())
sweets = []
for _ in range(N):
K = int(sys.stdin.readline())
town_sweets = []
for _ in range(K):
a, b, c = map(int, sys.stdin.readline().split())
town_sweets.append((a, b, c))
def compare(s1, s2):
a1, b1, _ = s1
a2, b2, _ = s2
val = b1 * a2 - b2 * a1
return -1 if val > 0 else 1 if val < 0 else 0
town_sweets.sort(key=cmp_to_key(compare))
sweets.append(town_sweets)
d = []
for _ in range(N):
row = list(map(int, sys.stdin.readline().split()))
d.append(row)
dp = [[] for _ in range(N)]
def add_state(t, y_new, c_new, s_new):
for y, c, s in dp[t]:
if y <= y_new and c <= c_new and s >= s_new:
return False
new_entries = []
added = False
for entry in dp[t]:
y, c, s = entry
if not (y >= y_new and c >= c_new and s <= s_new):
new_entries.append(entry)
new_entries.append((y_new, c_new, s_new))
if len(new_entries) == len(dp[t]):
return False
dp[t] = new_entries
return True
add_state(0, 0, 0, 0)
queue = deque([(0, 0, 0, 0)])
while queue:
t, y, c, s = queue.popleft()
for next_town in range(N):
move_cost = d[t][next_town]
new_cost_move = c + move_cost
if new_cost_move > X:
continue
Y_available = Y - y
X_available = X - new_cost_move
available = min(Y_available, X_available)
if available < 0:
continue
sum_a = 0
sum_b = 0
remaining = available
for a, b, c_sweet in sweets[next_town]:
if remaining <= 0:
break
max_q = min(c_sweet, remaining // a)
if max_q == 0:
continue
sum_a += max_q * a
sum_b += max_q * b
remaining -= max_q * a
new_y_total = y + sum_a
new_cost_total = new_cost_move + sum_a
new_sat = s + sum_b
if new_y_total > Y or new_cost_total > X:
continue
if add_state(next_town, new_y_total, new_cost_total, new_sat):
queue.append((next_town, new_y_total, new_cost_total, new_sat))
max_sat = max([s for y, c, s in dp[0]] + [0])
print(max_sat)
if __name__ == "__main__":
main()