#Q87. 「一本通 3.3 练习 2」虫洞 Wormholes

「一本通 3.3 练习 2」虫洞 Wormholes

Description

Original source: USACO 2006 Dec. Gold, original problem can be found at POJ 3259

While wandering around his farm, John discovered several wormholes. A wormhole can be considered a very peculiar directed edge that can transport you back to a moment in the past (relative to the time before you entered the wormhole). Each of John's farms consists of NN fields (labeled from 11 to NN) connected by MM bidirectional paths (undirected edges) and WW wormholes.

Now John wants to use these wormholes to go back in time (return to the starting point before the departure time). Please tell him if this is possible. John will provide you with the maps of FF farms. No path will take you more than 10410^4 seconds to traverse, and no wormhole will take you back more than 10410^4 seconds.

Input Format

The first line contains an integer FF, the number of farms;

For each farm:

The first line contains three integers N,M,WN, M, W;

The next MM lines each contain three integers S,E,TS, E, T, indicating a bidirectional path between field SS and field EE that takes TT seconds to traverse;

The next WW lines each contain three integers S,E,TS, E, T, indicating a wormhole from field SS to field EE that transports John TT seconds into the past.

Output Format

Output FF lines in total. For the ii-th farm, if John can achieve his goal, output YES on the ii-th line; otherwise, output NO.

Sample 1

2
3 3 1
1 2 2
1 3 4
2 3 1
3 1 3
3 2 1
1 2 3
2 3 4
3 1 8

NO
YES

Data Range and Hints

For all data, 1F51\le F\le 5, 1N5001\le N\le 500, 1M25001\le M\le 2500, 1W2001\le W\le 200, 1S,EN1\le S,E\le N, T104|T|\le 10^4.