Nomad Camp

시간 제한2.5초메모리 제한2048 MB

요약
각 정점이 네 가지 계절 유형 중 하나를 갖는 가중 그래프에서, 계절을 여러 번 바꿔 모든 사람을 한 목초지로 모을 수 있는지 판정한다. 한 번 바꿀 때마다 모든 목초지의 사람이 새 계절 유형의 가장 가까운 목초지로 이동하며, 거리가 같으면 번호가 작은 쪽을 고른다.
난이도

어려움10점 중 8점

유형
그래프, 최단 경로, BFS, 수학
정답자
아직 제출이 없습니다

문제

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 nn pastures, numbered from 11 to nn, and mm 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 TT (1≤T≤1041 \le T \le 10^4): the number of test cases. For each test case:

The first line contains two integers nn and mm (1≤n≤2001 \le n \le 200, 1≤m≤n⋅(n−1)21 \le m \le \frac{n \cdot (n - 1)}{2}): the number of pastures and the number of roads between them.

The second line contains nn integers c_1,c_2,…,c_nc\_1, c\_2, \ldots, c\_n (1≤c_i≤41 \le c\_i \le 4): the types of pastures.

Each of the next mm lines contains three integers, u_iu\_i, v_iv\_i, and w_iw\_i (1≤u_i,v_i≤n1 \le u\_i, v\_i \le n, 1≤w_i≤1051 \le w\_i \le 10^5), which mean there is a bidirectional road between pastures u_iu\_i and v_iv\_i that has length w_iw\_i.

It is guaranteed that the sum of nn for all test cases does not exceed 10510^5.

It is guaranteed that the sum of mm for all test cases does not exceed 10610^6.

출력

Output TT 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.

예제1

  1. 예제 1

    입력
    2
    4 4
    1 2 2 4
    1 2 5
    2 3 100
    3 4 8
    1 3 11
    7 9
    3 1 3 2 4 1 2
    3 5 7
    7 1 1
    1 2 7
    1 5 1
    4 7 10
    4 5 10
    5 2 11
    2 7 3
    3 4 10
    
    예상 출력
    YES
    NO