#BS0000401. Đường đi có trung bình nhỏ nhất (Minimum Average Path)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đường đi có trung bình nhỏ nhất (Minimum Average Path)
Minimum Average Path
Version: Phuoc Hung OJ Extended
PHOJ practice edition. Its constraints, input/output format, examples and judging rules are specified below.
Problem Statement
Given a directed acyclic graph (DAG) with vertices and integer-weighted edges, find a path from vertex 1 to vertex minimizing the arithmetic mean of its edge weights. At least one such path exists. Any optimal path is accepted.
Input
The first line contains . Each of the next lines contains , denoting a directed edge from to of weight . No ordered pair is repeated. Vertex labels need not be in topological order.
Output
Print the number of edges on the first line. On the second line print vertex labels in path order, starting at 1 and ending at . The mean must be exactly optimal; no floating-point tolerance is used.
Subtasks
- Subtask 1 (20 points): ; ; , ; ; the graph is a simple DAG with a path from 1 to .
- Subtask 2 (30 points): ; ; , ; ; the graph is a simple DAG with a path from 1 to .
- Subtask 3 (50 points): ; ; , ; ; the graph is a simple DAG with a path from 1 to .
Created examples
Input
4 4
1 2 10
2 4 2
1 3 4
3 4 4
Output
2
1 3 4
Explanation
There are exactly two paths from 1 to 4: through 2, with mean , and through 3, with mean . Choose the second path, which has 2 edges and vertices 1, 3, 4.
Input
3 3
1 2 5
2 3 5
1 3 5
Output
1
1 3
Explanation
The direct path has mean 5. The path has mean . Both are optimal; the sample chooses the direct path with one edge.
Input
5 4
1 4 0
4 2 0
2 5 1
1 5 1
Output
3
1 4 2 5
Explanation
The path has 3 edges of total weight , so its mean is . The direct path has mean 1. There are no other paths from 1 to 5, so the three-edge path is optimal.