#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 walk\textit{walk} is defined as a sequence of edges e1e_1, e2e_2, ⋯\cdots, ele_{l} where for each 1≤k<l1 \le k < l, vekv_{e_k} (the tail of edge eke_k) is the same as uek+1u_{e_{k+1}} (the head of edge ek+1e_{k+1}). We can say a walk e1,e2,⋯ ,ele_1, e_2, \cdots, e_l starts at vertex ue1u_{e_1} and ends at vertex velv_{e_l}. Note that the same edge can appear multiple times in a walk.

The color sequence\textit{color sequence} of a walk e1,e2,⋯ ,ele_1, e_2, \cdots, e_l is defined as ce1,ce2,⋯ ,celc_{e_1}, c_{e_2}, \cdots, c_{e_l}.

Consider all color sequences of walks of length at most 1010010^{100} from vertex SS to vertex TT in GG. 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 MM lines follow: the jj (1≤j≤M1 \le j \le M)-th of them contains three space-separated integers uiu_i, viv_i and cic_i (1≤ui,vi≤N1 \le u_i, v_i \le N, ui≠viu_i \neq v_i, 1≤ci≤1091 \le c_i \le 10^{9}); it describes a directional edge from vertex uiu_i to vertex viv_i with color cic_i.

The graph doesn't have multiple edges and each edge has a unique color. Formally, for any 1≤i<j≤M1 \le i < j \le M, ci≠cjc_i \neq c_j and (ui,vi)≠(uj,vj)(u_i, v_i) \neq (u_j, v_j) holds.

输出格式

If there is no walk from vertex $S$ to vertex $T$, print $\texttt{IMPOSSIBLE}$. (without quotes)

Otherwise, let's say a1,a2,⋯ ,ala_1, a_2, \cdots, a_l is the lexicographically minimum sequence among all color sequences of length at most 1010010^{100} from vertex SS to vertex TT.

  • If l≤106l \le 10^{6}, print a1,a2,⋯ ,ala_1, a_2, \cdots, a_l in the first line. There should be a space between each printed integer.
  • If l>106l > 10^{6}, print TOO LONG\texttt{TOO LONG}. (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 jj (0≤j<min⁡(n,m)0 \le j < \min(n, m)) where p1=q1p_1 = q_1, p2=q2p_2 = q_2, ⋯\cdots, pj=qjp_{j} = q_{j} and pj+1<qj+1p_{j+1} < q_{j+1}.
  • n<mn < m and p1=q1p_1 = q_1, p2=q2p_2 = q_2, ⋯\cdots, pn=qnp_n = q_n. In other words, pp is a strict prefix of qq. 标签: P14618|图论|贪心|2019|高校校赛

来源

P14618|[2019 KAIST RUN Fall] Lexicographically Minimum Walk