CF1851E.Nastya and Potions

传统题 时间 3000 ms 内存 256 MiB 9 尝试 10 已通过 8 标签

Nastya and Potions

Statement

Alchemist Nastya loves mixing potions. There are a total of nn types of potions, and one potion of type ii can be bought for cic_i coins. Any kind of potions can be obtained in no more than one way, by mixing from several others. The potions used in the mixing process will be consumed. Moreover, no potion can be obtained from itself through one or more mixing processes. As an experienced alchemist, Nastya has an unlimited supply of kk types of potions p1,p2,,pkp_1, p_2, \dots, p_k, but she doesn't know which one she wants to obtain next. To decide, she asks you to find, for each 1in1 \le i \le n, the minimum number of coins she needs to spend to obtain a potion of type ii next.

Input

The first line of each test contains an integer tt (1t1041 \le t \le 10^4) — the number of test cases. Each test case is described as follows: The first line contains two integers nn and kk (1k<n21051 \le k \lt n \le 2 \cdot 10^5) — the total number of potion types and the number of potion types Nastya already has. The second line contains nn integers c1,c2,,cnc_1, c_2, \dots, c_n (1ci1091 \le c_i \le 10^9) — the costs of buying the potions. The third line contains kk distinct integers p1,p2,,pkp_1, p_2, \dots, p_k (1pin1 \le p_i \le n) — the indices of potions Nastya already has an unlimited supply of. This is followed by nn lines describing ways to obtain potions by mixing. Each line starts with the integer mim_i (0mi<n0 \le m_i \lt n) — the number of potions required to mix a potion of the type ii (1in1 \le i \le n). Then line contains mim_i distinct integers e1,e2,,emie_1, e_2, \dots, e_{m_i} (1ejn1 \le e_j \le n, ejie_j \ne i) — the indices of potions needed to mix a potion of the type ii. If this list is empty, then a potion of the type ii can only be bought. It is guaranteed that no potion can be obtained from itself through one or more mixing processes. It is guaranteed that the sum of all nn values across all test cases does not exceed 21052 \cdot 10^5. Similarly, it is guaranteed that the sum of all mim_i values across all test cases does not exceed 21052 \cdot 10^5.

Output

For each test case, output nn integers — the minimum number of coins Nastya needs to spend to obtain a potion of each type.

样例

样例 1

输入:

4
5 1
30 8 3 5 10
3
3 2 4 5
0 
0 
2 3 5
0 
3 2
5 143 3
1 3
1 2
0 
2 1 2
5 1
5 4 1 3 4
2
2 4 5
3 3 5 4
2 1 4
1 5
0 
4 2
1 1 5 4
2 4
3 2 4 3
0 
2 2 4
1 2

输出:

23 8 0 5 10 
0 143 0 
5 0 1 3 4 
0 0 0 0 

样例 2

输入:

3
6 3
5 5 4 5 2 2
3 4 5
2 2 5
1 5
3 4 1 6
4 2 6 1 5
0 
0 
6 2
1 4 4 1 5 2
3 6
4 6 3 4 5
4 6 5 3 4
0 
1 5
1 6
0 
2 1
4 3
1
0 
1 1

输出:

0 0 0 0 0 2 
0 0 0 0 0 0 
0 0 

样例解释(英文原文)

In the first test case of the first sample, it is optimal:

  • Get a potion of the first type by buying and mixing 22, 44 and 55;
  • a potion of the second type can only be obtained by purchasing it;
  • Nastya already has an unlimited number of potions of the third type;
  • a potion of the fourth type is more profitable to buy than to buy and mix other potions;
  • a potion of the fifth type can only be obtained by purchasing it.

在线编程 IDE

建议全屏模式获得最佳体验