Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <queue>
- #include <algorithm>
- using namespace std;
- typedef long long ll;
- typedef pair <ll, ll> ii;
- const ll Inf = 9000000000000000000ll;
- const int Maxn = 12;
- const int Maxm = 1000;
- int t;
- int n, m;
- int st, en;
- int rw[Maxm], rs1[Maxm], rs2[Maxm];
- ll work[1 << Maxn];
- int getSet()
- {
- int res = 0;
- int siz; scanf("%d", &siz);
- while (siz--) {
- int a; scanf("%d", &a);
- res |= 1 << a;
- }
- return res;
- }
- int main()
- {
- scanf("%d", &t);
- for (int tc = 1; tc <= t; tc++) {
- scanf("%d %d", &n, &m);
- st = getSet();
- en = getSet();
- for (int i = 0; i < m; i++) {
- scanf("%d", &rw[i]); rs1[i] = getSet(); rs2[i] = getSet();
- }
- fill(work, work + (1 << n), Inf);
- work[st] = 0;
- priority_queue <ii> Q; Q.push(ii(0, st));
- while (!Q.empty()) {
- int mask = Q.top().second;
- ll d = -Q.top().first; Q.pop();
- if (d != work[mask]) continue;
- for (int i = 0; i < m; i++)
- if ((mask & rs1[i]) == rs1[i]) {
- int cand = (mask ^ rs1[i]) | rs2[i];
- if (d + rw[i] < work[cand]) {
- work[cand] = d + rw[i];
- Q.push(ii(-work[cand], cand));
- }
- }
- }
- printf("Case #%d: ", tc);
- if (work[en] == Inf) printf("NO\n");
- else printf("YES %I64d\n", work[en]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment