Lexicographically Minimum Walk

S에서 T로 가는 길이 10^100 이하인 모든 워크 중 색 순열이 사전순으로 가장 작은 것을 찾고, 불가능하거나 10^6을 넘으면 해당 문구를 출력한다.

어려움8그래프그리디BFS정렬아직 제출이 없습니다시간 제한2초메모리 제한1024 MB

문제

There is a directed graph GG with NN nodes and MM edges. Each node is numbered 11 through NN, and each edge is numbered 11 through MM. For each ii (1iM1 \le i \le M),  edge ii goes from vertex u_iu\_i to vertex v_iv\_i and has a unique color c_ic\_i.

A walk is defined as a sequence of edges e_1e\_1, e_2e\_2, \cdots, e_le\_{l} where for each 1k<l1 \le k < l, v_e_kv\_{e\_k} (the tail of edge e_ke\_k) is the same as u_e_k+1u\_{e\_{k+1}} (the head of edge e_k+1e\_{k+1}). We can say a walk e_1, e_2, , e_le\_1,\ e\_2,\ \cdots,\ e\_l starts at vertex u_e_1u\_{e\_1} and ends at vertex v_e_lv\_{e\_l}. Note that the same edge can appear multiple times in a walk.

The color sequence of a walk e_1, e_2, , e_le\_1,\ e\_2,\ \cdots,\ e\_l is defined as c_e_1, c_e_2, , c_e_lc\_{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 NN, MM, SS, and TT (1N100,0001 \le N \le 100\\,000, 0M300,0000 \le M \le 300\\,000, 1SN1 \le S \le N, 1TN1 \le T \le N, STS \neq T).

Then MM lines follow: the ii (1iM1 \le i \le M)-th of them contains three space-separated integers u_iu\_i, v_iv\_i and c_ic\_i (1u_i,v_iN1 \le u\_i, v\_i \le N, u_iv_iu\_i \neq v\_i, 1c_i1091 \le c\_i \le 10^{9}); it describes a directional edge from vertex u_iu\_i to vertex v_iv\_i with color c_ic\_i.

The graph doesn't have multiple edges or loops, and each edge has a unique color. Formally, for any 1i<jM1 \le i < j \le M, c_ic_jc\_i \neq c\_j and (u_i, v_i)(u_j, v_j)(u\_i,\ v\_i) \neq (u\_j,\ v\_j) holds.

출력

If there is no walk from vertex SS to vertex TT, print "IMPOSSIBLE".  (without quotes)

Otherwise, let's say a_1, a_2, , a_la\_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 l106l \le 10^{6}, print a_1, a_2, , a_la\_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". (without quotes)

힌트

Sequence p_1,p_2,,p_np\_1, p\_2, \cdots, p\_{n} is lexicographically smaller than another sequence q_1,q_2,,q_mq\_1, q\_2, \cdots, q\_{m} if and only if one of the following holds:

  • There exists a unique jj (0j<min(n,m)0 \le j < \min(n, m)) where p_1=q_1p\_1 = q\_1, p_2=q_2p\_2 = q\_2, \cdots, p_j=q_jp\_{j} = q\_{j} and p_j+1<q_j+1p\_{j+1} < q\_{j+1}.
  • n<mn < m and p_1=q_1p\_1 = q\_1, p_2=q_2p\_2 = q\_2, \cdots, p_n=q_np\_n = q\_n. In other words, pp is a strict prefix of qq.