PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BF0000007. Lỗ sâu thời gian (Wormholes)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Lỗ sâu thời gian (Wormholes)

Wormholes

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

Determine whether a reachable directed negative cycle permits travel into the past.

A wormhole network is a directed graph with nn vertices numbered 00 through n−1n-1. Traveling along an edge u→vu\to v changes the time by an integer tt. A negative value means traveling backward through time. Every vertex is reachable from vertex 00.

Determine whether a traveler can follow a closed path and return to the starting place earlier than the departure time. This is possible exactly when the graph has a negative-weight cycle. Report only whether such a cycle exists; do not output its vertices.

Input

First line: n,mn,m. Next mm lines u,v,tu,v,t, vertices from 0 to n−1n-1.

Output

Print possible for a negative cycle, otherwise not possible.

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le20; 0≤m≤500\le m\le50; −1000≤w≤1000-1000\le w\le1000; all vertices reachable from 0; no self-loops or parallel directed edges.
  • Subtask 2 (30 points): 1≤n≤1501\le n\le150; 0≤m≤5000\le m\le500; −1000≤w≤1000-1000\le w\le1000; all vertices reachable from 0; no self-loops or parallel directed edges.
  • Subtask 3 (50 points): 1≤n≤10001\le n\le1000; 0≤m≤20000\le m\le2000; −1000≤w≤1000-1000\le w\le1000; all vertices reachable from 0; no self-loops or parallel directed edges.

Examples

Input

3 3
0 1 1
1 2 -5
2 1 1

Output

possible

Explanation

Relaxation finds eligible paths, or a negative cycle in the required scope. The sample uses the exact output tokens specified by the problem.