Painting the Roads
시간 제한1초메모리 제한2048 MB
각 간선의 목표 색이 주어진 트리에서 m개의 로봇이 주어진 도시에서 출발할 때, 검은색이어야 하는 간선만 홀수 번 지나도록 하는 최소 총 이동 거리를 구한다.
문제
You are the king of the Pigeon Kingdom. The Pigeon Kingdom consists of cities and 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 . Initially, each road is white. However, you think that it is too boring this way. So you have decided to assign robots to cities to paint the roads to your favorite color pattern. Different robots can be assigned to the same city. Robot starts at city , 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 robots, print instead.
입력
The first line contains an integer , the number of test cases (). The test cases follow.
The first line of each test case contains two integers and ( and ), denoting the number of cities and the number of robots, respectively.
Each of the next lines contains four integers , , , (; ; or ), denoting a road of length connecting cities and . If , 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 integers (), denoting the starting city for each robot.
It is guaranteed that the of sum of over all test cases will not exceed , and the sum of over all test cases will not exceed .
출력
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 instead.