Painting the Roads

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

요약
각 간선의 목표 색이 주어진 트리에서 m개의 로봇이 주어진 도시에서 출발할 때, 검은색이어야 하는 간선만 홀수 번 지나도록 하는 최소 총 이동 거리를 구한다.
난이도

어려움10점 중 8점

유형
트리, 동적 계획법, 그래프, 그리디
정답자
아직 제출이 없습니다

문제

You are the king of the Pigeon Kingdom. The Pigeon Kingdom consists of nn cities and n−1n-1 two-way roads, each connecting a pair of cities. It is guaranteed that it is possible to traverse between any two cities through the roads.

Each road has a color, black or white, and a length ℓ_i\ell\_i. Initially, each road is white. However, you think that it is too boring this way. So you have decided to assign mm robots to mm cities to paint the roads to your favorite color pattern. Different robots can be assigned to the same city. Robot ii starts at city p_ip\_i, travels through some roads (possibly none), and then stops. A robot cannot travel through one road multiple times. When a robot travels through a road, it flips the color of the road (if it was white, it turns black, and vice versa). The robots are independent, which means that they will not interfere with each other in the travel. We assume that different robots don't paint any road simultaneously. Also, different robots can stop in the same city. The cost of a robot's travel is defined as the sum of lengths of all the roads on its path.

As the king of the Pigeon Kingdom, you want to minimize the total cost of all the travels. If it is impossible to paint the roads to the desired pattern with the mm robots, print −1-1 instead.

입력

The first line contains an integer tt, the number of test cases (1≤t≤50001 \le t \le 5000). The test cases follow.

The first line of each test case contains two integers nn and mm (2≤n≤50002 \le n \le 5000 and 1≤m≤50001 \le m \le 5000), denoting the number of cities and the number of robots, respectively.

Each of the next n−1n-1 lines contains four integers u_iu\_i, v_iv\_i, ℓ_i\ell\_i, c_ic\_i (1≤u_i<v_i≤n1 \le u\_i < v\_i \le n; 1≤ℓ_i≤101\le \ell\_i \le 10; c_i=0c\_i=0 or c_i=1c\_i=1), denoting a road of length ℓ_i\ell\_i connecting cities u_iu\_i and v_iv\_i. If c_i=0c\_i=0, you should paint it white in the desired pattern; otherwise, you should paint it black. It is guaranteed that it is possible to traverse between any pair of cities through the given roads.

Then a single line contains mm integers p_jp\_j (1≤p_j≤n1 \le p\_j \le n), denoting the starting city for each robot.

It is guaranteed that the of sum of nn over all test cases will not exceed 50005000, and the sum of mm over all test cases will not exceed 50005000.

출력

For each test case, print one line containing a single integer: the minimal total cost to paint all the roads to the desired pattern. If it is impossible to do so, print −1-1 instead.

예제1

  1. 예제 1

    입력
    5
    3 2
    1 2 1 1
    2 3 2 1
    1 3
    4 2
    1 2 3 1
    2 3 1 0
    3 4 4 1
    1 2
    5 4
    1 2 3 0
    2 3 1 1
    3 4 2 0
    4 5 2 1
    1 1 1 1
    5 2
    1 2 2 1
    1 3 3 0
    1 5 2 1
    3 4 1 1
    1 2
    10 5
    1 2 10 1
    2 3 3 1
    3 4 4 0
    4 5 4 1
    5 6 2 1
    2 7 8 0
    2 8 9 1
    4 9 1 0
    1 10 4 0
    10 10 2 1 8
    
    예상 출력
    3
    9
    21
    -1
    42