CF1851E.Nastya and Potions
普及/提高-
通过率:0%
时间限制:3.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Alchemist Nastya loves mixing potions. There are a total of n types of potions, and one potion of type i can be bought for ci 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 k types of potions p1,p2,…,pk, but she doesn't know which one she wants to obtain next. To decide, she asks you to find, for each 1≤i≤n, the minimum number of coins she needs to spend to obtain a potion of type i next.
炼金术士娜斯佳喜欢调配魔药。总共有 n 种魔药,其中第 i 种魔药的购买价格为 ci 枚金币。
任意一种魔药最多只能通过一种方式(即由若干其他魔药混合而成)获得。混合过程中所用的魔药将被消耗掉。此外,任何魔药都无法通过一次或多次混合过程从其自身获得。
作为一名经验丰富的炼金术士,娜斯佳拥有 k 种魔药 p1,p2,…,pk 的无限供应,但她尚不确定下一步想要获得哪一种魔药。为了帮助她做决定,请你对每个 1≤i≤n,计算出娜斯佳为获得第 i 种魔药所需花费的最少金币数。
输入格式
The first line of each test contains an integer t (1≤t≤104) — the number of test cases.
Each test case is described as follows:
The first line contains two integers n and k (1≤k<n≤2⋅105) — the total number of potion types and the number of potion types Nastya already has.
The second line contains n integers c1,c2,…,cn (1≤ci≤109) — the costs of buying the potions.
The third line contains k distinct integers p1,p2,…,pk (1≤pi≤n) — the indices of potions Nastya already has an unlimited supply of.
This is followed by n lines describing ways to obtain potions by mixing.
Each line starts with the integer mi (0≤mi<n) — the number of potions required to mix a potion of the type i (1≤i≤n).
Then line contains mi distinct integers e1,e2,…,emi (1≤ej≤n, ej=i) — the indices of potions needed to mix a potion of the type i. If this list is empty, then a potion of the type i 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 n values across all test cases does not exceed 2⋅105. Similarly, it is guaranteed that the sum of all mi values across all test cases does not exceed 2⋅105.
每组测试数据的第一行包含一个整数 t(1≤t≤104)—— 测试用例的数量。
每个测试用例的描述如下:
第一行包含两个整数 n 和 k(1≤k<n≤2⋅105)—— 魔药种类的总数,以及 Nastya 已经拥有的魔药种类数量。
第二行包含 n 个整数 c1,c2,…,cn(1≤ci≤109)—— 购买各魔药的成本。
第三行包含 k 个互不相同的整数 p1,p2,…,pk(1≤pi≤n)—— Nastya 已拥有无限供应的魔药的编号(下标)。
接下来是 n 行,描述通过混合方式获取魔药的方法。
每一行以整数 mi(0≤mi<n)开头—— 表示合成第 i 种魔药(1≤i≤n)所需魔药的数量。
随后该行包含 mi 个互不相同的整数 e1,e2,…,emi(1≤ej≤n,且 ej=i)—— 表示合成第 i 种魔药所需的魔药编号。若该列表为空,则第 i 种魔药仅能通过购买获得。
保证不存在任何一种魔药可通过一次或多次混合过程由其自身得到。
保证所有测试用例中 n 的总和不超过 2⋅105;同样地,保证所有测试用例中所有 mi 的总和也不超过 2⋅105。
输出格式
For each test case, output n integers — the minimum number of coins Nastya needs to spend to obtain a potion of each type.
对于每个测试用例,输出 n 个整数——Nastya 获得每种药水所需的最少硬币数。
输入输出样例
输入#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
输出#1
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
输出#2
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 2, 4 and 5;
- 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.
在第一个样例的第一个测试用例中,最优策略是:
- 通过购买并混合原料 2、4 和 5 来获得第一种药水;
- 第二种药水只能通过直接购买获得;
- Nastya 已经拥有无限数量的第三种药水;
- 第四种药水直接购买比购买其他原料再混合更划算;
- 第五种药水只能通过直接购买获得。
输入解题思路,AI测评打分。不知道怎么写?