欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1851E.Nastya and Potions
Nastya and Potions
Statement
Alchemist Nastya loves mixing potions. There are a total of types of potions, and one potion of type can be bought for 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 types of potions , but she doesn't know which one she wants to obtain next. To decide, she asks you to find, for each , the minimum number of coins she needs to spend to obtain a potion of type next.
Input
The first line of each test contains an integer () — the number of test cases. Each test case is described as follows: The first line contains two integers and () — the total number of potion types and the number of potion types Nastya already has. The second line contains integers () — the costs of buying the potions. The third line contains distinct integers () — the indices of potions Nastya already has an unlimited supply of. This is followed by lines describing ways to obtain potions by mixing. Each line starts with the integer () — the number of potions required to mix a potion of the type (). Then line contains distinct integers (, ) — the indices of potions needed to mix a potion of the type . If this list is empty, then a potion of the type 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 values across all test cases does not exceed . Similarly, it is guaranteed that the sum of all values across all test cases does not exceed .
Output
For each test case, output 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 , and ;
- 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
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |