Under construction

Still being built. Things may change

Shortest Path or Negative Cycle

Interview

Time limit4sMemory limit128 MB

Summary
Given a directed weighted graph and a source vertex s, output a negative cycle if one exists, otherwise the shortest distance from s to every vertex.
Level

Medium4 of 10

Topics
Shortest path, Graph
Solved
No attempts yet

Problem

A graph with NN vertices and MM edges is given. The vertices are numbered 0,1,…,N−10, 1, \ldots, N-1. A vertex ss is also given. For every vertex 0≤i≤N−10 \le i \le N-1, there is a path from ss to ii.

Edge ii goes from vertex aia_i to vertex bib_i and has weight cic_i. cic_i may be negative.

If the graph has a negative cycle, return any one of them.

If the graph has no negative cycle, print the length of the shortest path from ss to tt for every vertex 0≤t≤N−10 \le t \le N-1.

Input

The first line contains the integers NN, MM, and ss.

Each of the next MM lines contains aia_i, bib_i, and cic_i.

Output

If the graph has a negative cycle:

  • The first line prints CYCLE.
  • The second line prints kk, the number of edges in the negative cycle. k≥1k \ge 1.
  • The third line prints k+1k+1 integers u0,u1,…,uku_0, u_1, \ldots, u_k. uiu_i and ui+1u_{i+1} are the start and end vertices of the ii-th edge of the cycle. u0=uku_0 = u_k, and the cycle includes each edge at most once.

If the graph has no negative cycle:

  • The first line prints PATH.
  • The second line prints NN integers dist0,dist1,…,distN−1dist_0, dist_1, \ldots, dist_{N-1}, where distidist_i is the sum of weights on the shortest path from ss to ii.

Constraints

  • 2≤N≤20 0002 \le N \le 20\,000
  • 1≤M≤20 0001 \le M \le 20\,000
  • 0≤s<N0 \le s < N
  • 0≤ai,bi≤N0 \le a_i, b_i \le N
  • (ai,bi)≠(aj,bj)(a_i, b_i) \neq (a_j, b_j) (i≠ji \neq j)
  • −104≤ci≤104-10^4 \le c_i \le 10^4
  • For every vertex 0≤i≤N−10 \le i \le N-1, there is a path from ss to ii.

Examples2

  1. Example 1

    Input
    3 4 0
    0 1 4
    0 2 3
    1 2 -1
    2 0 -2
    
    Expected output
    PATH
    0 4 3
    
  2. Example 2

    Input
    3 4 0
    0 1 4
    0 2 3
    1 2 -4
    2 0 -2
    
    Expected output
    CYCLE
    3
    0 1 2 0