SmallR is a biologist. Her latest research finding is how to change the sex of dogs. In other words, she can change female dogs into male dogs and vice versa.
She is going to demonstrate this technique. Now SmallR has n dogs, the costs of each dog's change may be different. The dogs are numbered from 1 to n. The cost of change for dog i is vi RMB. By the way, this technique needs a kind of medicine which can be valid for only one day. So the experiment should be taken in one day and each dog can be changed at most once.
This experiment has aroused extensive attention from all sectors of society. There are m rich folks which are suspicious of this experiment. They all want to bet with SmallR forcibly. If SmallR succeeds, the i-th rich folk will pay SmallR wi RMB. But it's strange that they have a special method to determine whether SmallR succeeds. For i-th rich folk, in advance, he will appoint certain ki dogs and certain one gender. He will think SmallR succeeds if and only if on some day the ki appointed dogs are all of the appointed gender. Otherwise, he will think SmallR fails.
If SmallR can't satisfy some folk that isn't her friend, she need not pay him, but if someone she can't satisfy is her good friend, she must pay g RMB to him as apologies for her fail.
Then, SmallR hope to acquire money as much as possible by this experiment. Please figure out the maximum money SmallR can acquire. By the way, it is possible that she can't obtain any money, even will lose money. Then, please give out the minimum money she should lose.
The first line contains three integers n, m, g (1 ≤ n ≤ 10^4, 0 ≤ m ≤ 2000, 0 ≤ g ≤ 10^4). The second line contains n integers, each is 0 or 1, the sex of each dog, 0 represent the female and 1 represent the male. The third line contains n integers v1, v2, ..., vn (0 ≤ vi ≤ 10^4).
Each of the next m lines describes a rich folk. On the i-th line the first number is the appointed sex of i-th folk (0 or 1), the next two integers are wi and ki (0 ≤ wi ≤ 10^4, 1 ≤ ki ≤ 10), next ki distinct integers are the indexes of appointed dogs (each index is between 1 and n). The last number of this line represents whether i-th folk is SmallR's good friend (0 — no or 1 — yes).
Print a single integer, the maximum money SmallR can gain. Note that the integer is negative if SmallR will lose money.
5 5 9
0 1 1 1 0
1 8 6 2 3
0 7 3 3 2 1 1
1 8 1 5 1
1 0 3 2 1 4 1
0 8 3 4 2 1 0
1 7 2 4 1 1
2
5 5 8
1 0 1 1 1
6 5 4 2 8
0 6 3 2 3 4 0
0 8 3 3 2 4 0
0 0 3 3 4 1 1
0 10 3 4 3 1 1
0 4 3 3 4 1 1
题目大意:有n个点,每个一开始是白色或者黑色。可以花v i 的代价改变第i个点的颜色。
有m条件,每个条件都是要求某一些点都是某种颜色。如果满足了第i个条件可以得到wi的收益,没有满足则须付出g的代价。求最大收益
分析:经典的最大权闭合子图模型.
一开始所有的点都有颜色. 如果第i个点是白色,则从S连一条边到点i,边权为vi,割掉这条边就表示将颜色变成黑色. 对于黑色点,则连向T,边权为vi.
将每个人也看作点. 如果第j个人的要求是白色点,则从S连一条边到j,边权为wi + g(加不加g取决于j是不是特殊人),并且j连向它要求的所有的点,边权为inf.
如果j要求的是黑色点,则j连向T,并且j要求的点都连向j.
为什么要这么做呢?考虑割每一类边的意义. 每个人和其要求的点之间的边是不能割的,这是题目的限制.
对于要求为白色点的人,因为源点直接连向了它,所以它连向的白点与源点之间的连边不会被割,只有黑点与汇点之间的边会被割.
对于要求为黑色点的人同样如此.
最后的答案就是总的收益-最小割.
#include <cstdio> #include <queue> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 70010,inf = 0x7fffffff; int n,m,g,S,T,sex[maxn],v[maxn],ans; int head[maxn],to[maxn],nextt[maxn],w[maxn],tot = 2,d[maxn],cur[maxn]; void add(int x,int y,int z) { w[tot] = z; to[tot] = y; nextt[tot] = head[x]; head[x] = tot++; w[tot] = 0; to[tot] = x; nextt[tot] = head[y]; head[y] = tot++; } bool bfs() { memset(d,-1,sizeof(d)); d[S] = 0; queue <int> q; q.push(S); while (!q.empty()) { int u = q.front(); q.pop(); if(u == T) return true; for (int i = head[u];i;i = nextt[i]) { int v = to[i]; if(w[i] && d[v] == -1) { d[v] = d[u] + 1; q.push(v); } } } return false; } int dfs(int u,int f) { if (u == T) return f; int res = 0; for (int i = cur[u];i;i = nextt[i]) { int v = to[i]; if(w[i] && d[v] == d[u] + 1) { int temp = dfs(v,min(f - res,w[i])); w[i] -= temp; w[i ^ 1] += temp; res += temp; if (w[i]) cur[u] = i; if (res == f) return res; } } if (!res) d[u] = -1; return res; } void dinic() { while (bfs()) { for (int i = 1; i <= T; i++) cur[i] = head[i]; ans -= dfs(S,inf); } } int main() { scanf("%d%d%d",&n,&m,&g); S = n + m + 1; T = n + m + 2; for (int i = 1; i <= n; i++) scanf("%d",&sex[i]); for (int i = 1; i <= n; i++) scanf("%d",&v[i]); for (int i = 1; i <= n; i++) { if (sex[i] == 0) add(S,i,v[i]); else add(i,T,v[i]); } for (int i = 1; i <= m; i++) { int sexx,wi,num,flag; scanf("%d%d%d",&sexx,&wi,&num); ans += wi; for (int j = 1; j <= num; j++) { int temp; scanf("%d",&temp); if (sexx == 1) add(temp,i + n,inf); else add(i + n,temp,inf); } scanf("%d",&flag); if (flag) wi += g; if (sexx == 1) add(i + n,T,wi); else add(S,i + n,wi); } dinic(); printf("%d ",ans); return 0; }