Problem p01130 - Generation 2

Orig Description

Water Pipe Construction
In the year 21XX, humanity finally began a plan to migrate to Mars. You, who were selected as the first wave of immigrants to Mars, were assigned to the Administrative Center of Mars and are dealing with various problems that occur on Mars. The biggest problem at the moment for the administrative center is to secure a self-sufficient supply and demand cycle. Since it takes several months for aid supplies from the moon to arrive, it is essential to solve the needs on Mars within Mars. In addition, until a circulation system is established, resources must be conserved as much as possible.
The Administrative Center has started mining ice in the polar regions. The ultimate goal is to melt it with solar energy and supply it to each base as water. In the first stage, they decided to lay water pipes from bases with water sources to the two main bases. Also, at present, only some bases and roads connecting them have been developed, and laying water pipes in undeveloped areas requires a significant cost and fuel along the roads, so they decided to lay water pipes along the roads. Furthermore, due to technological constraints, their water pipes always flow in only one direction.
Your job is to create a program that minimizes the cost of laying water pipes under these conditions.
Input
The input consists of multiple datasets.
The first line of each dataset consists of five integers in order: the number of bases n on Mars (3 <= n <= 100), the number of roads m (2 <= m <= 1000) connecting the bases, the number s of the base that serves as the water source, and the numbers g1 and g2 of the two main bases that are the destinations of the water pipes. The base numbers are represented by integers from 1 to n. s, g1, and g2 are distinct.
The following m lines provide information on roads where water pipes can be laid. Each line represents information on a road between two bases, consisting of three integers b1, b2, and c (1 <= c <= 1000). Here, b1 and b2 represent the base numbers at the start and end of the road, respectively, and they are distinct. c is the cost required to lay a water pipe from base b1 to base b2.
For all datasets, it can be assumed that there is always a path to supply water from the source to the destination. Moreover, since there is at most one road between any two bases, cost information is given at most once for each direction.
The end of the input is indicated by a line containing five zeros separated by spaces.
Output
For each dataset, output the minimum cost to lay water pipes on one line.
Sample Input
4 5 1 3 4
1 2 5
2 3 5
2 4 5
1 3 8
1 4 8
0 0 0 0 0
Output for the Sample Input
15

Extracted Specification

An integer n (3 ≤ n ≤ 100), an integer m (2 ≤ m ≤ 1000), an integer s (1 ≤ s ≤ n), and two distinct integers g1 and g2 (1 ≤ g1, g2 ≤ n). Followed by m lines, each containing three integers b1, b2 (1 ≤ b1, b2 ≤ n, b1 ≠ b2), and c (1 ≤ c ≤ 1000). Input ends with a line containing five zeros.

### Example Input:

```
4 5 1 3 4
1 2 5
2 3 5
2 4 5
1 3 8
1 4 8
0 0 0 0 0
```

### Function Signature:
Write a function f(inputs) that takes in the input as a list of tuples.
def f(inputs: List[Tuple[int, int, int, int, int, List[Tuple[int, int, int]]]]): 
    ''' 
    inputs: a list of tuples
    '''

Test Case Generator

Sure, let's write a test case generator for the "Water Pipe Construction" problem. This generator will create random datasets that follow the problem's constraints. Here’s the Python code for the test case generator:

```python
import random

def generate_test_case():
    # Number of bases (3 <= n <= 100)
    n = random.randint(3, 100)
    
    # Number of roads (2 <= m <= 1000)
    m = random.randint(2, 1000)
    
    # Source base (1 <= s <= n)
    s = random.randint(1, n)
    
    # Two distinct destination bases (1 <= g1 < g2 <= n)
    g1 = random.randint(1, n)
    g2 = random.randint(1, n)
    while g1 == g2 or g1 == s or g2 == s:
        g1 = random.randint(1, n)
        g2 = random.randint(1, n)
    
    # Generate m roads with distinct bases and cost (1 <= c <= 1000)
    roads = set()
    while len(roads) < m:
        b1 = random.randint(1, n)
        b2 = random.randint(1, n)
        while b1 == b2:
            b2 = random.randint(1, n)
        if (b1, b2) not in roads:
            c = random.randint(1, 1000)
            roads.add((b1, b2, c))
    
    # Print the generated test case
    print(f"{n} {m} {s} {g1} {g2}")
    for road in roads:
        print(f"{road[0]} {road[1]} {road[2]}")
    print("0 0 0 0 0")

# Generate multiple test cases
for _ in range(5):  # Generate 5 test cases
    generate_test_case()
```

This code will generate five test cases for the "Water Pipe Construction" problem. Each test case will have the following structure:

1. The first line contains five integers: the number of bases `n`, the number of roads `m`, the source base `s`, and the two destination bases `g1` and `g2`.
2. The next `m` lines each contain three integers representing a road between two bases and the cost to lay a water pipe on that road.
3. Finally, a line with "0 0 0 0 0" indicates the end of the input.

Note that this generator ensures the constraints are met by generating valid inputs. You can adjust the number of test cases or the ranges as needed.

Extract Arguments

def extract_arguments(fh):
    datasets = []
    while True:
        first_line = list(map(int, fh.readline().strip().split()))
        if first_line == [0, 0, 0, 0, 0]:
            break
        n, m, s, g1, g2 = first_line
        roads = []
        for _ in range(m):
            b1, b2, c = map(int, fh.readline().strip().split())
            roads.append((b1, b2, c))
        datasets.append((n, m, s, g1, g2, roads))
    return datasets

# if __name__ == "__main__":
#     input_path = sys.argv[1]
#     with open(input_path, 'r') as fh: 
#         datasets = extract_arguments(fh)
#         for data in datasets:
#             f(*data)