Orig Description
G: Quest of Merchant
To achieve good results in ICPC, training is essential. Rabbit wants to win in ICPC, so he decided to train today as well.
Today's training is to run around the town and trade, to gain the power of business.
For future training, Rabbit wants to earn as much money as possible.
In this world, the roads are lined up at equal intervals in the north, south, east, and west directions, making a grid pattern. The only market is located at (0, 0), and the coordinates of x and y are determined for the town (the points corresponding to the intersections correspond to the integer values of the coordinates). Rabbit can only move along the road and takes 1 minute to move between adjacent intersections. There are some towns at the intersections. In trading, he buys goods in the town and sells them at the market to gain the profit of the price difference.
Rabbit has enough initial capital, so he never runs out of money and cannot buy goods. However, each product has a weight, and Rabbit can only carry goods up to a total weight of W at a time. Therefore, he visits town to purchase goods and returns to the market. Depending on the situation, he may purchase goods in multiple towns before returning to the market.
Buying goods in town and selling them in the market can be done in an instant. Also, it is possible to purchase an infinite number of goods, and there is no possibility that the goods in the town will be sold out.
Rabbit has compiled data on the names, weights per unit, and selling prices of each product he wishes to trade this time, as well as the names, x and y coordinates, and selling prices of the products sold in each town. Rabbit is now at the market (0, 0). Using a program, he wants to find out how much he can earn in T minutes.
Input
N M W T
S1 V1 P1
...
SM VM PM
L1 X1 Y1
R1,1 Q1,1
...
R1,L1 Q1,L1
...
LN XN YN
Extracted Specification
An integer N (1 ≤ N ≤ 100), an integer M (1 ≤ M ≤ 100), an integer W (1 ≤ W ≤ 1000), and an integer T (1 ≤ T ≤ 10000).
A list of M tuples, where each tuple contains a string Si (1 ≤ |Si| ≤ 100), an integer Vi (1 ≤ Vi ≤ 100), and an integer Pi (1 ≤ Pi ≤ 1000).
A list of N tuples, where each tuple contains an integer Li (1 ≤ Li ≤ 100), an integer Xi (-1000 ≤ Xi ≤ 1000), and an integer Yi (-1000 ≤ Yi ≤ 1000).
For each tuple in the list of N tuples, there is a list of Li tuples, where each tuple contains a string Ri,j (1 ≤ |Ri,j| ≤ 100) and an integer Qi,j (1 ≤ Qi,j ≤ 1000).
### Example Input:
```
2 3 50 100
apple 2 30
banana 5 60
cherry 1 10
1 5 5
item1 100
2 -5 -5
item2 200
item3 300
```
### Function Signature:
Write a function f(N, M, W, T, items, towns) that takes in the input:
```
def f(N: int, M: int, W: int, T: int, items: List[Tuple[str, int, int]], towns: List[Tuple[int, int, int, List[Tuple[str, int]]]]):
'''
N: an integer
M: an integer
W: an integer
T: an integer
items: a list of tuples, each containing a string and two integers
towns: a list of tuples, each containing two integers, an integer, and a list of tuples containing a string and an integer
'''
```
Test Case Generator
Sure! Let's break down the problem and create a test case generator for "G: Quest of Merchant".
### Problem Statement Breakdown
- **Input:**
- \(N\) (number of towns)
- \(M\) (number of different products)
- \(W\) (maximum weight Rabbit can carry)
- \(T\) (total available time in minutes)
- For each product:
- \(S_i\) (name of the product)
- \(V_i\) (weight per unit of the product)
- \(P_i\) (selling price per unit of the product)
- For each town:
- \(L_i\) (name of the town)
- \(X_i\) (x-coordinate of the town)
- \(Y_i\) (y-coordinate of the town)
- For each product sold in the town:
- \(R_{i,j}\) (name of the product)
- \(Q_{i,j}\) (quantity of the product available)
- **Output:**
- The maximum profit Rabbit can earn in \(T\) minutes.
### Test Case Generator
The generator will randomize the number of towns, products, weights, and coordinates. We will ensure that all inputs are within reasonable competitive programming constraints.
Here's the Python code to generate such test cases:
```python
import random
import string
def generate_random_string(length=5):
return ''.join(random.choices(string.ascii_uppercase, k=length))
def tcgen():
# Constraints
MAX_N = 100
MAX_M = 50
MAX_W = 1000
MAX_T = 10000
N = random.randint(1, MAX_N)
M = random.randint(1, MAX_M)
W = random.randint(1, MAX_W)
T = random.randint(1, MAX_T)
# Products information
products = []
for _ in range(M):
S = generate_random_string()
V = random.randint(1, 100)
P = random.randint(1, 1000)
products.append((S, V, P))
# Towns information
towns = []
for _ in range(N):
L = generate_random_string()
X = random.randint(-100, 100)
Y = random.randint(-100, 100)
town_products = []
num_products_in_town = random.randint(1, M)
for _ in range(num_products_in_town):
R = random.choice(products)[0]
Q = random.randint(1, 100)
town_products.append((R, Q))
towns.append((L, X, Y, town_products))
# Format output
input_data = f"{N} {M} {W} {T}\n"
for S, V, P in products:
input_data += f"{S} {V} {P}\n"
for L, X, Y, town_products in towns:
input_data += f"{L} {X} {Y}\n"
for R, Q in town_products:
input_data += f"{R} {Q}\n"
return input_data
# Example of generating a test case
print(tcgen())
```
### Explanation
- **generate_random_string:** Generates random product or town names.
- **tcgen:** Generates random values for towns, products, and their attributes, and aggregates them into a formatted input string.
- **N, M, W, T:** Randomly chosen within set constraints.
- **Product details:** Randomly generated names, weights, and prices.
- **Town details:** Randomly generated names, coordinates, and associated products with their quantities.
By running `tcgen()`, we get a test case formatted in the required input format for the problem. Each run will generate different test cases, suitable for thorough testing of the solution.
Extract Arguments
def extract_arguments(fh):
N, M, W, T = map(int, fh.readline().strip().split())
products = []
for _ in range(M):
data = fh.readline().strip().split()
name = data[0]
weight = int(data[1])
price = int(data[2])
products.append((name, weight, price))
towns = []
for _ in range(N):
data = fh.readline().strip().split()
town_name = data[0]
town_x = int(data[1])
town_y = int(data[2])
selling_prices = []
for _ in range(int(data[3])):
product_name, product_price = fh.readline().strip().split()
product_price = int(product_price)
selling_prices.append((product_name, product_price))
towns.append((town_name, town_x, town_y, selling_prices))
return N, M, W, T, products, towns