Orig Description
Problem D: One-Time Path
Problem
There are N islands and M bridges.
Each island is assigned a number from 1 to N.
Each bridge is assigned a number from 1 to M.
Gaccho is currently on the first island at time 0.
By using the i-th bridge, Gaccho can move unidirectionally from island ai to island bi.
However, at time 0, all the bridges are submerged underwater because of high tide.
The i-th bridge can be crossed when the tide goes out at time ci.
Once the tide comes in again after time ci, the bridge i will be submerged again, and Gaccho will assume that he can never cross the bridge again.
Gaccho wants to enjoy the scenery from island 1 to N-1 for as long as possible, but since he has a boat parked on island N, he must reach island N in the end.
Also, since he is waiting for his parents on the boat, Gaccho must leave for home as soon as he arrives at island N.
Since the time Gaccho spends crossing bridges or moving around islands is very short, it can be assumed to be 0.
Find the maximum time Gaccho can spend on any of the islands between 1 and N-1.
If Gaccho cannot move to island N no matter how he moves, output -1 instead.
Input
The input is given in the following format:
N M
a1 b1 c1
a2 b2 c2
…
aM bM cM
The first line contains two integers N and M separated by a space.
For each of the next M lines, three integers ai, bi, and ci are given separated by a space.
Constraints
2 ≤ N ≤ 105
1 ≤ M ≤ 2 × 105
1 ≤ ai < N
1 ≤ bi ≤ N
1 ≤ ci ≤ 109
ai ≠ bi
Output
If G
Extracted Specification
An integer N (2 ≤ N ≤ 10^5), representing some quantity or size.
An integer M (1 ≤ M ≤ 2 × 10^5), representing some quantity or size.
A list of M tuples (ai, bi, ci), each with three integers:
- ai (1 ≤ ai < N)
- bi (1 ≤ bi ≤ N)
- ci (1 ≤ ci ≤ 10^9)
### Example Input:
```
4 5
1 2 3
2 3 4
3 4 5
1 3 6
2 4 7
```
### Function Signature:
Write a function f(N, M, bridges) that takes in the input.
```
def f(N: int, M: int, bridges: List[Tuple[int, int, int]]):
'''
N: an integer
M: an integer
bridges: a list of tuples, each containing three integers
'''
```
Test Case Generator
Certainly! Below is the test case generator for the specified problem "One-Time Path".
```python
import random
def tcgen():
# Number of islands (N) and bridges (M)
N = random.randint(2, 10**5)
M = random.randint(1, 2 * 10**5)
bridges = []
for _ in range(M):
ai = random.randint(1, N-1)
bi = random.randint(1, N)
while bi == ai: # Ensure ai ≠ bi
bi = random.randint(1, N)
ci = random.randint(1, 10**9)
bridges.append((ai, bi, ci))
return N, M, bridges
# Example usage:
N, M, bridges = tcgen()
print(N, M)
for bridge in bridges:
print(bridge[0], bridge[1], bridge[2])
```
### Explanation:
1. **N (Number of Islands):** Randomly chosen between 2 and \(10^5\).
2. **M (Number of Bridges):** Randomly chosen between 1 and \(2 \times 10^5\).
3. **Bridges:**
- Each bridge has a start island \(a_i\) and an end island \(b_i\) with \(1 \leq a_i < N\) and \(1 \leq b_i \leq N\).
- Ensure \(a_i \neq b_i\).
- The crossing time \(c_i\) is randomly chosen between 1 and \(10^9\).
This generator creates random test cases within the constraints specified, ensuring that the input data is valid. You can run the above script to generate test cases for the problem.
Extract Arguments
def extract_arguments(fh):
N, M = map(int, fh.readline().strip().split())
bridges = []
for _ in range(M):
ai, bi, ci = map(int, fh.readline().strip().split())
bridges.append((ai, bi, ci))
return N, M, bridges
# if __name__ == "__main__":
# input_path = sys.argv[1]
# with open(input_path, 'r') as fh:
# N, M, bridges = extract_arguments(fh)
# f(N, M, bridges)