Problem p01704 - Generation 2

Orig Description

Problem StatementWe have planted $N$ flower seeds, all of which come into different flowers.
We want to make all the flowers come out together.
Each plant has a value called vitality, which is initially zero.
Watering and spreading fertilizers cause changes on it, and the $i$-th plant will come into flower if its vitality is equal to or greater than $\mathit{th}_i$.
Note that $\mathit{th}_i$ may be negative because some flowers require no additional nutrition.
Watering effects on all the plants.
Watering the plants with $W$ liters of water changes the vitality of the $i$-th plant by $W \times \mathit{vw}_i$ for all $i$ ($1 \le i \le n$), and costs $W \times \mathit{pw}$ yen, where $W$ need not be an integer.
$\mathit{vw}_i$ may be negative because some flowers hate water.
We have $N$ kinds of fertilizers, and the $i$-th fertilizer effects only on the $i$-th plant.
Spreading $F_i$ kilograms of the $i$-th fertilizer changes the vitality of the $i$-th plant by $F_i \times \mathit{vf}_i$, and costs $F_i \times \mathit{pf}_i$ yen, where $F_i$ need not be an integer as well.
Each fertilizer is specially made for the corresponding plant, therefore $\mathit{vf}_i$ is guaranteed to be positive.
Of course, we also want to minimize the cost.
Formally, our purpose is described as "to minimize $W \times \mathit{pw} + \sum_{i=1}^{N}(F_i \times \mathit{pf}_i)$ under $W \times \mathit{vw}_i + F_i \times \mathit{vf}_i \ge \mathit{th}_i$, $W \ge 0$, and $F_i \ge 0$ for all $i$ ($1 \le i \le N$)".
Your task is to calculate the minimum cost.
Input
The input consists of multiple datasets.
The number of datasets does not exceed $100$, and the data size of the input does not exceed $20\mathrm{MB}$.
Each dataset is formatted as follows.
$N$$\mathit{pw}$$\mathit{vw}_1$ $\mathit{pf}_1$ $\mathit{vf}_1$ $\mathit{th}_1$::$\mathit{vw}_N$ $\mathit{pf}_N$ $\mathit{vf}_N$ $\mathit{th}_N$
The first line of a dataset contains a single integer $N$, number of flower seeds.
The second line of a dataset contains a single integer $\mathit{pw}$, cost of watering one liter.
Each of the following $N$ lines describes a flower. The $i$-th line contains four integers, $\mathit{vw}_i$, $\mathit{pf}_i$, $\mathit{vf}_i$, and $\mathit{th}_i$, separated by a space.
You can assume that $1 \le N \le 10^5$, $1 \le \mathit{pw} \le 100$, $-100 \le \mathit{vw}_i \le 100$, $1 \le \mathit{pf}_i \le 100$, $1 \le \mathit{vf}_i \le 100$, and $-100 \le \mathit{th}_i \le 100$.
The end of the input is indicated by a line containing a zero.
Output
For each dataset, output a line containing the minimum cost to make all the flowers come out.
The output must have an absolute or relative error at most $10^{-4}$.
Sample Input
3
10
4 3 4 10
5 4 5 20
6 5 6 30
3
7
-4 3 4 -10
5 4 5 20
6 5 6 30
3
1
-4 3 4 -10
-5 4 5 -20
6 5 6 30
3
10
-4 3 4 -10
-5 4 5 -20
-6 5 6 -30
0
Output for the Sample Input
43.5
36
13.5
0

Extracted Specification

An integer T, representing the number of datasets. Each dataset consists of:
- An integer N (1 ≤ N ≤ 10^5), representing some quantity or size.
- An integer X (1 ≤ X ≤ 100), representing a cost factor.
- N lines, each containing four integers: 
  - Y (−100 ≤ Y ≤ 100), a value related to the quantity.
  - Z (1 ≤ Z ≤ 100), another cost factor.
  - W (1 ≤ W ≤ 100), another value related to the quantity.
  - V (−100 ≤ V ≤ 100), a threshold value.

The end of the input is indicated by a line containing a zero.

### Example Input:

```
3
10
4 3 4 10
5 4 5 20
6 5 6 30
3
7
-4 3 4 -10
5 4 5 20
6 5 6 30
3
1
-4 3 4 -10
-5 4 5 -20
6 5 6 30
3
10
-4 3 4 -10
-5 4 5 -20
-6 5 6 -30
0
```

### Function Signature:
Write a function f(datasets) that processes the input list of datasets.
```python
def f(datasets: List[Tuple[int, int, List[Tuple[int, int, int, int]]]]):
    '''
    datasets: a list of tuples, each representing a dataset
        - each tuple contains:
            - an integer N
            - an integer X
            - a list of N tuples, each containing four integers (Y, Z, W, V)
    '''
```

Test Case Generator

To generate test cases for the given problem statement, we need to create datasets that match the input format described while ensuring that all constraints are respected. Here is the test case generator for the problem:

```python
import random

def tcgen():
    # Generate random datasets
    datasets = []
    num_datasets = random.randint(1, 100)

    for _ in range(num_datasets):
        # Number of flower seeds N (1 ≤ N ≤ 10^5)
        N = random.randint(1, 10**5)
        
        # Cost of watering one liter pw (1 ≤ pw ≤ 100)
        pw = random.randint(1, 100)
        
        # List to store flower data
        flowers = []
        
        for _ in range(N):
            # vw_i (-100 ≤ vw_i ≤ 100)
            vw_i = random.randint(-100, 100)
            
            # pf_i (1 ≤ pf_i ≤ 100)
            pf_i = random.randint(1, 100)
            
            # vf_i (1 ≤ vf_i ≤ 100)
            vf_i = random.randint(1, 100)
            
            # th_i (-100 ≤ th_i ≤ 100)
            th_i = random.randint(-100, 100)
            
            flowers.append((vw_i, pf_i, vf_i, th_i))
        
        datasets.append((N, pw, flowers))

    return datasets

def format_datasets(datasets):
    formatted_data = []
    for dataset in datasets:
        N, pw, flowers = dataset
        formatted_data.append(str(N))
        formatted_data.append(str(pw))
        for flower in flowers:
            formatted_data.append(" ".join(map(str, flower)))
    
    formatted_data.append("0")  # End of input indicator
    return "\n".join(formatted_data)

# Example usage
if __name__ == "__main__":
    datasets = tcgen()
    formatted_data = format_datasets(datasets)
    print(formatted_data)
```

### Explanation:

1. **Function `tcgen()`**:
   - Generates a random number of datasets (`num_datasets`), each between 1 and 100.
   - For each dataset:
     - Generates a random number of flower seeds \(N\) between 1 and \(10^5\).
     - Generates a random cost of watering one liter \(pw\) between 1 and 100.
     - For each of the \(N\) flowers, generates:
       - \(vw_i\) (water effect) between -100 and 100.
       - \(pf_i\) (cost of \(i\)-th fertilizer) between 1 and 100.
       - \(vf_i\) (vitality change by \(i\)-th fertilizer) between 1 and 100.
       - \(th_i\) (threshold for blooming) between -100 and 100.
   - Collects and returns all datasets.

2. **Function `format_datasets()`**:
   - Takes the generated datasets and formats them into the required input format as a single string.
   - Adds an end-of-input indicator (`"0"`) at the end.

You can run this script to generate random test cases that you can use to test your solution.

Extract Arguments

def extract_arguments(fh):
    datasets = []
    while True:
        N = int(fh.readline().strip())
        if N == 0:
            break
        pw = int(fh.readline().strip())
        flowers = []
        for _ in range(N):
            vw_i, pf_i, vf_i, th_i = map(int, fh.readline().strip().split())
            flowers.append((vw_i, pf_i, vf_i, th_i))
        datasets.append((N, pw, flowers))
    return datasets