Orig Description
Princess in Danger
A tomboyish and brave princess from a poor country was supposed to marry another country for political marriage. However, while on her way to the country she was going to marry, she was attacked by a villain who tried to kill her, causing her to be seriously injured and transported to a nearby hospital. However, the villain used a special poison to make sure the princess was dead. Therefore, to save the princess, it was necessary to urgently transport a special medicine and frozen blood from her relatives from the home country.
The blood is transported in a frozen state, but to keep it fresh, it must be refrozen at a blood freezing facility within a maximum of M minutes from the previous freezing. However, there are only a few places where freezing facilities are installed.
Blood is safe for M minutes from sufficient freezing. If the remaining time to avoid refreezing is S minutes and transported for T minutes without freezing, the remaining time that does not require refreezing becomes S - T minutes. The remaining time that does not require refreezing will recover up to a maximum of M minutes by refreezing. The time required to refreeze the blood depends on how much it is frozen. For every 1 minute the blood is frozen at the freezing facility, the remaining time that does not need to be refrozen recovers by 1 minute.
At the time of departure from the capital of the home country, the remaining time for the blood not to be refrozen is M minutes.
As a servant of the princess, your mission is to calculate the shortest route from the capital of the home country to the hospital where the princess was transported, to transport the blood while keeping it fresh. So, your mission is to find the shortest time from the capital of the home country to the hospital.
Input
The input consists of multiple data sets. The first line of each data set contains six non-negative integers N (2 ≦ N ≦ 100), M (1 ≦ M ≦ 100), L (0 ≦ L ≦ N - 2), K, A (0 ≦ A < N), and H (0 ≦ H < N). These represent the number of cities, the time limit for re-freezing, the number of cities with freezing facilities, the number of roads directly connecting cities, the number of the capital of the home country, and the number of the city where the hospital where the princess was transported is located, respectively. The capital of the home country and the hospital where the princess was transported are different cities. It is assumed that each city is assigned a number from 0 to N-1. The next line contains L non-negative integers separated by a single space, which represent the number of the city with a freezing facility. Note that the capital of the home country and the hospital where the princess was transported are not included in this list, but it is considered that there is a freezing facility. In the next K lines, information on roads connecting cities is given. The i-th line contains three non-negative integers X, Y, and T separated by a single space, indicating that there is a road directly connecting the city X and the city Y, and it takes T time to travel. Note that this road is bi-directional. Also, there is at most one road directly connecting city X and city Y.
The input ends with N=M=L=K=A=H= 0, which is not included in the data set.
Output
For each data set, output the shortest time it takes to deliver the blood while keeping it fresh. If it cannot be delivered, output "Help!".
Sample Input
2 1 0 1 0 1
0 1 2
3 1 1 2 0 1
2
0 2 1
1 2 1
3 2 1 2 0 1
2
0 2 1
1 2 1
4 4 1 4 1 3
2
0 1 2
1 2 4
0 2 1
3 0 3
5 3 2 6 0 3
1 2
2 1 2
1 0 1
3 4 1
2 4 1
4 1 2
2 0 2
5 4 2 6 0 3
1 2
4 2 4
2 1 2
4 3 1
0 1 5
1 4 2
2 0 3
0 0 0 0 0 0
Output for the Sample Input
Help!
3
2
10
5
12
Extracted Specification
An integer N (2 ≤ N ≤ 100), representing some quantity.
An integer M (1 ≤ M ≤ 100), representing some quantity.
An integer L (0 ≤ L ≤ N - 2), representing some quantity.
An integer K, representing some quantity.
An integer A (0 ≤ A < N), representing some quantity.
An integer H (0 ≤ H < N), representing some quantity.
A list of L non-negative integers each between 0 and N-1, representing some quantities.
K lines, each containing three non-negative integers X, Y, and T, where (0 ≤ X, Y < N) and T is an integer, representing some quantities.
### Example Input:
```
2 1 0 1 0 1
0 1 2
3 1 1 2 0 1
2
0 2 1
1 2 1
3 2 1 2 0 1
2
0 2 1
1 2 1
4 4 1 4 1 3
2
0 1 2
1 2 4
0 2 1
3 0 3
5 3 2 6 0 3
1 2
2 1 2
1 0 1
3 4 1
2 4 1
4 1 2
2 0 2
5 4 2 6 0 3
1 2
4 2 4
2 1 2
4 3 1
0 1 5
1 4 2
2 0 3
0 0 0 0 0 0
```
### Function Signature:
Write a function `f(inputs)` that processes the list of tuples.
```python
def f(inputs: List[Tuple[int, int, int, int, int, int, List[int], List[Tuple[int, int, int]]]]):
'''
inputs: a list of tuples where each tuple contains:
- an integer N
- an integer M
- an integer L
- an integer K
- an integer A
- an integer H
- a list of L integers
- a list of K tuples each containing three integers
'''
```
Test Case Generator
Sure, I can help you write a test case generator for the "Princess in Danger" problem. Here is a Python script to generate random test cases for this problem:
```python
import random
def generate_test_case():
# Generate random values for N, M, L, K, A, H
N = random.randint(2, 100)
M = random.randint(1, 100)
L = random.randint(0, N-2)
K = random.randint(1, (N*(N-1))//2) # Maximum number of roads is N*(N-1)/2
A = random.randint(0, N-1)
H = random.randint(0, N-1)
# Ensure A and H are different
while H == A:
H = random.randint(0, N-1)
# Generate the cities with freezing facilities
freezing_facilities = random.sample([i for i in range(N) if i != A and i != H], L)
# Generate the roads with random travel times
roads = set()
while len(roads) < K:
X = random.randint(0, N-1)
Y = random.randint(0, N-1)
if X != Y and (X, Y) not in roads and (Y, X) not in roads:
T = random.randint(1, 100)
roads.add((X, Y, T))
# Convert roads set to list for easier handling
roads = list(roads)
return N, M, L, K, A, H, freezing_facilities, roads
def print_test_case(N, M, L, K, A, H, freezing_facilities, roads):
print(f"{N} {M} {L} {K} {A} {H}")
if freezing_facilities:
print(" ".join(map(str, freezing_facilities)))
for X, Y, T in roads:
print(f"{X} {Y} {T}")
def main():
num_cases = random.randint(1, 10) # Generate between 1 and 10 test cases
for _ in range(num_cases):
test_case = generate_test_case()
print_test_case(*test_case)
print() # Print an empty line between test cases
print("0 0 0 0 0 0") # End of input
if __name__ == "__main__":
main()
```
### Explanation
1. **Random Value Generation**:
- `N`: Number of cities (2 to 100).
- `M`: Time limit for re-freezing (1 to 100).
- `L`: Number of cities with freezing facilities (0 to N-2).
- `K`: Number of roads (1 to N*(N-1)/2), ensuring there are at least one road.
- `A`: Capital city (0 to N-1).
- `H`: Hospital city (0 to N-1), ensuring it's different from `A`.
2. **Freezing Facilities**:
- A list of `L` cities with freezing facilities, excluding `A` and `H`.
3. **Roads**:
- Generate `K` unique roads with random travel times (1 to 100). Ensures no duplicate roads and no road connecting a city to itself.
4. **Output**:
- Print the test cases in the specified format.
- Print a terminating line "0 0 0 0 0 0" to indicate the end of input.
This script will generate test cases that fit the problem constraints and can be used to test the solution effectively.
Extract Arguments
def extract_arguments(fh):
data_sets = []
while True:
line = fh.readline().strip()
if line == "0 0 0 0 0 0":
break
N, M, L, K, A, H = map(int, line.split())
freezing_facilities = list(map(int, fh.readline().strip().split()))
roads = []
for _ in range(K):
X, Y, T = map(int, fh.readline().strip().split())
roads.append((X, Y, T))
data_sets.append((N, M, L, K, A, H, freezing_facilities, roads))
return data_sets
# if __name__ == "__main__":
# input_path = sys.argv[1]
# with open(input_path, 'r') as fh:
# data_sets = extract_arguments(fh)
# for data_set in data_sets:
# f(*data_set)