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.
To solve this problem, we need to maximize the satisfaction Gaccho gets from buying sweets within the constraints of his allowance and the maximum allowed amount for sweets. The solution involves dynamic programming and graph traversal to efficiently explore possible paths and purchases.
Approach
Precompute Shortest Paths: Use the Floyd-Warshall algorithm to precompute the shortest paths between all pairs of towns. This helps in determining the minimal travel cost between any two towns efficiently.
Knapsack for Each Town: For each town, precompute the maximum satisfaction possible for each possible amount of money spent (up to the maximum allowed for sweets, Y). This is done using a dynamic programming approach to solve the bounded knapsack problem, considering the stock limits of each sweet.
Dynamic Programming with BFS: Use a breadth-first search (BFS) approach to explore all possible states, tracking the current town, the total money spent on sweets, and the total travel cost. For each state, check if returning to the starting town (town 1) is feasible and update the maximum satisfaction accordingly.
Solution Code
python
import sys
from collections import deque
def main():
input = sys.stdin.read().split()
ptr = 0
N, X, Y = map(int, input[ptr:ptr+3])
ptr +=3
towns = []
for _ in range(N):
K = int(input[ptr])
ptr +=1
sweets = []
for __ in range(K):
a, b, c = map(int, input[ptr:ptr+3])
ptr +=3
sweets.append((a, b, c))
towns.append(sweets)
# Read distance matrix
dist = []
for _ in range(N):
row = list(map(int, input[ptr:ptr+N]))
ptr += N
dist.append(row)
# Floyd-Warshall to compute minimal distances
for k in range(N):
for i in range(N):
for j in range(N):
if dist[i][k] + dist[k][j] < dist[i][j]:
dist[i][j] = dist[i][k] + dist[k][j]
# Precompute knapsack for each town
town_knapsack = []
for sweets in towns:
dp = [0]*(Y+1)
for a, b, c in sweets:
# Bounded knapsack with binary optimization
k = 1
remaining = c
while k <= remaining:
cost = a * k
val = b * k
if cost > Y:
break
for s in range(Y, cost-1, -1):
if dp[s - cost] + val > dp[s]:
dp[s] = dp[s - cost] + val
remaining -= k
k *= 2
if remaining > 0:
cost = a * remaining
val = b * remaining
if cost > Y:
continue
for s in range(Y, cost-1, -1):
if dp[s - cost] + val > dp[s]:
dp[s] = dp[s - cost] + val
town_knapsack.append(dp)
max_sat = 0
# DP state: (current_town, sum_s, sum_move)
# Using 0-based index for towns
dp = [[[-1 for _ in range(X+1)] for __ in range(Y+1)] for ___ in range(N)]
dp[0][0][0] = 0
queue = deque()
queue.append((0, 0, 0))
while queue:
current, sum_s, sum_move = queue.popleft()
current_sat = dp[current][sum_s][sum_move]
# Check if returning home is possible
cost_home = dist[current][0]
total_move = sum_move + cost_home
total_cost = sum_s + total_move
if sum_s <= Y and total_cost <= X:
if current_sat > max_sat:
max_sat = current_sat
# Explore next towns
for next_town in range(N):
move_cost = dist[current][next_town]
new_move = sum_move + move_cost
if new_move > X - sum_s:
continue
# Try all possible s for next_town
for s_add in range(0, Y - sum_s +1):
sat_add = town_knapsack[next_town][s_add]
new_s = sum_s + s_add
new_total_cost = new_move + new_s
if new_s > Y or new_total_cost > X:
continue
if sat_add ==0 and s_add >0:
continue
new_sat = current_sat + sat_add
if new_sat > dp[next_town][new_s][new_move]:
dp[next_town][new_s][new_move] = new_sat
queue.append( (next_town, new_s, new_move) )
print(max_sat)
if __name__ == '__main__':
main()
Explanation
Floyd-Warshall Algorithm: This precomputes the shortest path between all pairs of towns, ensuring that travel costs are minimized.
Bounded Knapsack: For each town, the maximum satisfaction for each possible spending amount (up to Y) is precomputed using dynamic programming with binary optimization to handle stock limits efficiently.
BFS-based Dynamic Programming: This explores all possible states (current town, total money spent on sweets, total travel cost) to find the maximum satisfaction while adhering to the constraints. The BFS ensures efficient exploration of states, and each state transition considers moving to another town and purchasing sweets there.
This approach efficiently balances the exploration of possible paths and purchases, ensuring that the constraints are respected while maximizing satisfaction.