#6873. [2019 KAIST RUN Fall] Lexicographically Minimum Walk
[2019 KAIST RUN Fall] Lexicographically Minimum Walk
[2019 KAIST RUN Fall] Lexicographically Minimum Walk
题目描述
There is a directed graph $G$ with $N$ nodes and $M$ edges. Each node is numbered $1$ through $N$, and each edge is numbered $1$ through $M$. For each $i$ ($1 \le i \le M$), edge $i$ goes from vertex $u_i$ to vertex $v_i$ and has a $\textbf{unique}$ color $c_i$.A is defined as a sequence of edges , , , where for each , (the tail of edge ) is the same as (the head of edge ). We can say a walk starts at vertex and ends at vertex . Note that the same edge can appear multiple times in a walk.
The of a walk is defined as .
Consider all color sequences of walks of length at most from vertex to vertex in . Write a program that finds the lexicographically minimum sequence among them.
输入格式
The first line of the input contains four space-separated integers $N$, $M$, $S$, and $T$ ($1 \le N \le 100\,000$, $0 \le M \le 300\,000$, $1 \le S \le N$, $1 \le T \le N$, $S \neq T$).Then lines follow: the ()-th of them contains three space-separated integers , and (, , ); it describes a directional edge from vertex to vertex with color .
The graph doesn't have multiple edges and each edge has a unique color. Formally, for any , and holds.
输出格式
If there is no walk from vertex $S$ to vertex $T$, print $\texttt{IMPOSSIBLE}$. (without quotes)Otherwise, let's say is the lexicographically minimum sequence among all color sequences of length at most from vertex to vertex .
- If , print in the first line. There should be a space between each printed integer.
- If , print . (without quotes)
3 3 1 3
1 2 1
2 3 7
1 3 5
1 7
3 4 1 3
1 2 1
2 1 2
2 3 7
1 3 5
TOO LONG
2 0 2 1
IMPOSSIBLE
提示
Sequence $p_1, p_2, \cdots, p_{n}$ is lexicographically smaller than another sequence $q_1, q_2, \cdots, q_{m}$ if and only if one of the following holds:- There exists a unique () where , , , and .
- and , , , . In other words, is a strict prefix of . 标签: P14618|图论|贪心|2019|高校校赛