Orig Description
Problem I: How to Create a Good Game
A video game company called ICPC (International Company for Playing and Competing) is now developing a new arcade game. The new game features a lot of branches. This makes the game enjoyable for everyone, since players can choose their routes in the game depending on their skills. Rookie players can choose an easy route to enjoy the game, and talented players can choose their favorite routes to get high scores.
In the game, there are many checkpoints connected by paths. Each path consists of several stages, and completing stages on a path leads players to the next checkpoint. The game ends when players reach a particular checkpoint. At some checkpoints, players can choose which way to go, so routes diverge at that time. Sometimes different routes join together at a checkpoint. The paths between checkpoints are directed, and there is no loop (otherwise, players can play the game forever). In other words, the structure of the game can be viewed as a DAG (directed acyclic graph), when considering paths between checkpoints as directed edges.
Recently, the development team completed the beta version of the game and received feedbacks from other teams. They were quite positive overall, but there are some comments that should be taken into consideration. Some testers pointed out that some routes were very short compared to the longest ones. Indeed, in the beta version, the number of stages in one play can vary drastically depending on the routes. Game designers complained many brilliant ideas of theirs were unused in the beta version because of the tight development schedule. They wanted to have more stages included in the final product.
However, it’s not easy to add more stages. this is an arcade game – if the playing time was too long, it would bring down the income of the game and owners of arcades would complain. So, the longest route of the final product can’t be longer than that of the beta version. Moreover, the producer of the game didn’t want to change the structure of paths (i.e., how the checkpoints connect to each other), since it would require rewriting the scenario, recording voices, creating new cutscenes, etc.
Considering all together, the producer decided to add as many new stages as possible, while keeping the maximum possible number of stages in one play and the structure of paths unchanged. How many new stages can be added to the game?
Input
N M
x1 y1 s1
.
.
.
xM yM sM
The first line of the input contains two positive integers N and M (2 ≤ N ≤ 100, 1 ≤ M ≤ 1000). N indicates the number of checkpoints, including the opening and ending of the game. M indicates the number of paths between checkpoints.
The following M lines describe the structure of paths in the beta version of the game. The i-th line contains three integers xi , yi and si (0 ≤ xi < yi ≤ N - 1, 1 ≤ si ≤ 1000), which describe that there is a path from checkpoint xi to yi consists of si stages. As for indices of the checkpoints, note that 0 indicates the opening of the game and N - 1 indicates the ending of the game. You can assume that, for every checkpoint i, there exists a route from the opening to the ending which passes through checkpoint i. You can also assume that no two paths connect the same pair of checkpoints.
Output
Output a line containing the maximum number of new stages that can be added to the game under the following constraints:
You can’t increase the maximum possible number of stages in one play (i.e., the length of the longest route to the ending).
You can’t change the structure of paths (i.e., how the checkpoints connect to each other).
You can’t delete any stage that already exists in the beta version.
Sample Input 1
3 3
0 1 5
1 2 3
0 2 2
Output for the Sample Input 1
6
Sample Input 2
2 1
0 1 10
Output for the Sample Input 2
0
Sample Input 3
4 6
0 1 5
0 2 5
0 3 5
1 2 5
1 3 5
2 3 5
Output for the Sample Input 3
20
Extracted Specification
An integer N (2 ≤ N ≤ 100), representing some quantity or size.
An integer M (1 ≤ M ≤ 1000), representing another quantity or size.
A list of M tuples (xi, yi, si) where each tuple consists of:
- An integer xi (0 ≤ xi < N)
- An integer yi (xi < yi ≤ N - 1)
- An integer si (1 ≤ si ≤ 1000)
### Example Input:
```
3 3
0 1 5
1 2 3
0 2 2
```
### Function Signature:
Write a function f(N, M, paths) that takes in the input.
```
def f(N: int, M: int, paths: List[Tuple[int, int, int]]):
'''
N: an integer
M: an integer
paths: a list of tuples, where each tuple consists of three integers
'''
```
Test Case Generator
Certainly! Below is a test case generator for the given "Problem I: How to Create a Good Game". This generator will produce random test cases that adhere to the constraints of the problem statement.
```python
import random
def generate_test_case():
N = random.randint(2, 100) # Number of checkpoints (2 ≤ N ≤ 100)
M = random.randint(1, 1000) # Number of paths (1 ≤ M ≤ 1000)
edges = set()
while len(edges) < M:
x = random.randint(0, N - 2)
y = random.randint(x + 1, N - 1)
s = random.randint(1, 1000) # Number of stages in the path (1 ≤ si ≤ 1000)
edges.add((x, y, s))
edges = list(edges)
# Ensure that every checkpoint i has a route from the opening (0) to the ending (N-1)
# We'll add a simple path from 0 to N-1 to guarantee connectivity
if not any(edge[0] == 0 and edge[1] == N - 1 for edge in edges):
edges.append((0, N - 1, random.randint(1, 1000)))
# Ensure no two paths connect the same pair of checkpoints
edges = list(dict(((x, y), s) for x, y, s in edges).items())
# Reconstruct the final edges
final_edges = [(x, y, s) for (x, y), s in edges]
# Make sure the number of edges does not exceed M
final_edges = final_edges[:M]
return N, len(final_edges), final_edges
def print_test_case(N, M, edges):
print(N, M)
for edge in edges:
print(f"{edge[0]} {edge[1]} {edge[2]}")
# Generate and print a test case
N, M, edges = generate_test_case()
print_test_case(N, M, edges)
```
This test case generator performs the following steps:
1. Randomly determines the number of checkpoints `N` and paths `M` within their respective constraints.
2. Generates `M` unique paths `(xi, yi, si)` ensuring no two paths connect the same pair of checkpoints.
3. Ensures that every checkpoint `i` is reachable from the opening (checkpoint 0) and can reach the ending (checkpoint `N-1`).
4. Adds a mandatory path from checkpoint 0 to checkpoint `N-1` if it doesn't exist to guarantee connectivity.
5. Outputs the generated test case in the required format.
You can run the generator multiple times to produce different test cases for the problem.
Extract Arguments
def extract_arguments(fh):
N, M = map(int, fh.readline().strip().split())
paths = [tuple(map(int, fh.readline().strip().split())) for _ in range(M)]
return N, M, paths
# if __name__ == "__main__":
# input_path = sys.argv[1]
# with open(input_path, 'r') as fh:
# N, M, paths = extract_arguments(fh)
# f(N, M, paths)