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

#BF0000008. Lỗ sâu và đường bộ (Wormholes)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Lỗ sâu và đường bộ (Wormholes)

Wormholes

Source: BOJ

Version: Phuoc Hung OJ Extended

Problem Statement

Detect a negative cycle anywhere in a graph of positive two-way roads and negative one-way wormholes.

There are nn locations, mm ordinary roads and ww wormholes. An ordinary road connects two locations in both directions and takes tt time units either way. A wormhole is a one-way edge with travel time −t-t.

Determine whether some closed itinerary lets the traveler return to the departure point before leaving. No fixed source is specified, so a negative cycle in any component makes the answer YES. Treat each road description as two directed edges, but each wormhole as only one.

Input

First line n,m,wn,m,w. Then mm two-way roads u,v,tu,v,t and ww one-way wormholes u,v,tu,v,t with actual time −t-t.

Output

Print YES when any negative cycle exists, else NO.

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le20; 0≤m≤500\le m\le50; 0≤w≤2000\le w\le200; 1≤t≤100001\le t\le10000; two-way roads, one-way wormholes.
  • Subtask 2 (30 points): 1≤n≤1501\le n\le150; 0≤m≤5000\le m\le500; 0≤w≤2000\le w\le200; 1≤t≤100001\le t\le10000; two-way roads, one-way wormholes.
  • Subtask 3 (50 points): 1≤n≤5001\le n\le500; 0≤m≤25000\le m\le2500; 0≤w≤2000\le w\le200; 1≤t≤100001\le t\le10000; two-way roads, one-way wormholes.

Examples

Input

3 1 1
1 2 3
2 1 7

Output

YES

Explanation

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