原题链接:https://mirror.codeforces.com/contest/1851/problem/E
题意:
炼金术士娜斯佳喜欢调配药水。药水一共有 n种,其中 i种药水可以用 ci个金币购买。
任何一种药水都只能通过一种方法获得,即从其他几种药水中混合。在混合过程中使用的药水将被**消耗。此外,任何药剂都不能通过一种或多种混合过程从自身获得。
作为一名经验丰富的炼金术士,娜斯佳拥有k种药剂p1,p2,…,pk的无限供应,但她不知道下一步要获得哪一种。为了做出决定,她要求你找出每种 1≤i≤n1,她要获得下一种 i类型的药水至少需要花费多少硬币。
图论题,实现方式有点像spfa,当其中一种药水的费用消耗可以通过合成来减少时,则若不在队列中则将其入队,当一种药水的消耗减少时,有可能可以使其能够合成的药水费用减少,因此在输入时存单向边A->B表示A能作为合成B的原料之一,而拥有无限供应的药水则将它们的费用全部标为0.因为题目中表示药水不能通过任何途径合成回自己,因此保证无环,所以入队次数有限,复杂度大约为O(n+∑m+k).
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define LNF 1e18
#define INF 0x7fffffff
#define int long long
const db eps = 1e-9;
const int mod = 1e9 + 7;
const int MAXN = 2e5 + 5;
int n, m, k, h; string s;
int a[200005],num[200005],v[200005];
vector<int>f[200005];
void solve() {
int n;
cin >> n>>k;
for (int i = 1; i <= n; i++)cin >> a[i],f[i].clear();
for (int i = 1; i <= k; i++) {
int t; cin >> t; a[t] = 0;
}
for (int i = 1; i <= n; i++) {
int cnt; cin >> cnt;
if (cnt == 0)num[i] = LNF;
else num[i] = 0;
while (cnt--) {
int p; cin >> p;
num[i] += a[p]; f[p].push_back(i);
}
}
queue<int>q; for (int i = 1; i <= n; i++)q.push(i),v[i]=1;
while (!q.empty()) {
int pos = q.front(); q.pop(); v[pos] = 0;
if (num[pos] < a[pos]) {
for (auto i : f[pos]) {
num[i] =num[i]-a[pos]+num[pos];
if (!v[i]) {
q.push(i); v[i] = 1;
}
}a[pos] = num[pos];
}
}
for (int i = 1; i <= n; i++)cout << a[i] << " ";
cout << endl;
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//cout << gcd(4, 4);
int t; cin >> t; while (t--)
solve();
return 0;
}