Nomad Camp
시간 제한2.5초메모리 제한2048 MB
각 정점이 네 가지 계절 유형 중 하나를 갖는 가중 그래프에서, 계절을 여러 번 바꿔 모든 사람을 한 목초지로 모을 수 있는지 판정한다. 한 번 바꿀 때마다 모든 목초지의 사람이 새 계절 유형의 가장 가까운 목초지로 이동하며, 거리가 같으면 번호가 작은 쪽을 고른다.
문제
On summer vacation, Amir stayed at his grandmother's house, where she told him stories about how nomadic people in ancient times chose pastures for themselves:
There are only pastures, numbered from to , and roads available. Each pasture belongs to one of the four types: қыстау (winter), көктеу (spring), жайлау (summer), and күзеу (autumn).
Each pasture is initially inhabited by people, regardless of the season. When the season changes, from each pasture, people move to the nearest pasture corresponding to the new season. If there are multiple nearest pastures, they choose the pasture with the smallest number. If there is no pasture for the new season, people become sad and stop moving at all.
Now Amir wonders if it would be possible to gather all the people in one place if people could change the season of the year to any other season, as many times as they like.
입력
The first line contains a single integer (): the number of test cases. For each test case:
The first line contains two integers and (, ): the number of pastures and the number of roads between them.
The second line contains integers (): the types of pastures.
Each of the next lines contains three integers, , , and (, ), which mean there is a bidirectional road between pastures and that has length .
It is guaranteed that the sum of for all test cases does not exceed .
It is guaranteed that the sum of for all test cases does not exceed .
출력
Output lines, each of which is the answer to the corresponding test case. As the answer, output "YES" if it is possible to gather everyone in one place, and "NO" otherwise.