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), representing some quantity or size.
An integer m (2 ≤ m ≤ 1000), representing some quantity or size.
An integer s (1 ≤ s ≤ n), representing some quantity or size, distinct from g1 and g2.
An integer g1 (1 ≤ g1 ≤ n), representing some quantity or size, distinct from s and g2.
An integer g2 (1 ≤ g2 ≤ n), representing some quantity or size, distinct from s and g1.
A list of m tuples (b1, b2, c), where b1 and b2 are integers (1 ≤ b1, b2 ≤ n), distinct from each other, and c is an integer (1 ≤ c ≤ 1000).
### 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.
```python
from typing import List, Tuple
def f(inputs: List[Tuple[int, int, int, int, int, List[Tuple[int, int, int]]]]):
'''
inputs: a list of tuples where each tuple contains:
- an integer n
- an integer m
- an integer s
- an integer g1
- an integer g2
- a list of m tuples (b1, b2, c)
'''
```
Test Case Generator
Certainly! To create a test case generator for the provided "Water Pipe Construction" problem, we'll build a function that generates random datasets adhering to the given constraints.
Here's the implementation of the test case generator:
```python
import random
def tcgen():
datasets = []
while True:
n = random.randint(3, 100) # number of bases
m = random.randint(2, 1000) # number of roads
s = random.randint(1, n) # water source base number
g1, g2 = random.sample(range(1, n + 1), 2) # two distinct main bases
while g1 == s or g2 == s:
g1, g2 = random.sample(range(1, n + 1), 2)
dataset = [f"{n} {m} {s} {g1} {g2}"]
roads = set() # to avoid duplicate roads
for _ in range(m):
b1, b2 = random.sample(range(1, n + 1), 2)
while (b1, b2) in roads:
b1, b2 = random.sample(range(1, n + 1), 2)
c = random.randint(1, 1000) # cost of laying one road
dataset.append(f"{b1} {b2} {c}")
roads.add((b1, b2))
datasets.append("\n".join(dataset))
# Stop condition for generating multiple datasets
if random.random() < 0.1: # 10% chance to stop adding more datasets
break
datasets.append("0 0 0 0 0") # end marker
return "\n\n".join(datasets)
# Example execution
print(tcgen())
```
### Explanation
- **Parameter Generation**:
- `n` is generated between 3 and 100.
- `m` is generated between 2 and 1000.
- `s` is selected randomly between 1 and n.
- `g1` and `g2` are selected to be distinct from `s` and from each other.
- **Roads Generation**:
- Random pairs `(b1, b2)` are chosen, ensuring they are distinct and there are no duplicate roads.
- Random costs `c` between 1 and 1000 are assigned to each road.
- **Multiple Datasets**:
- The generator keeps adding datasets until a random stop condition is met (10% chance to stop).
- **End of Input**:
- The end is marked with `0 0 0 0 0` as specified.
This generator ensures all constraints are respected and provides a variety of test cases for robust testing.
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:
# inputs = extract_arguments(fh)
# f(inputs)