E. Biologist
time limit per test

1.5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Input

The first line contains three integers nmg (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).

Output

Print a single integer, the maximum money SmallR can gain. Note that the integer is negative if SmallR will lose money.

Examples
input
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
output
2
input
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
output
16
题目大意:有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 = ,inf = 0x7fffffff;
int n,m,g,S,T,sex[maxn],v[maxn],ans;
int head[maxn],to[maxn],nextt[maxn],w[maxn],tot = ,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] = ;
to[tot] = x;
nextt[tot] = head[y];
head[y] = tot++;
} bool bfs()
{
memset(d,-,sizeof(d));
d[S] = ;
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] == -)
{
d[v] = d[u] + ;
q.push(v);
}
}
}
return false;
} int dfs(int u,int f)
{
if (u == T)
return f;
int res = ;
for (int i = cur[u];i;i = nextt[i])
{
int v = to[i];
if(w[i] && d[v] == d[u] + )
{
int temp = dfs(v,min(f - res,w[i]));
w[i] -= temp;
w[i ^ ] += temp;
res += temp;
if (w[i])
cur[u] = i;
if (res == f)
return res;
}
}
if (!res)
d[u] = -;
return res;
} void dinic()
{
while (bfs())
{
for (int i = ; i <= T; i++)
cur[i] = head[i];
ans -= dfs(S,inf);
}
} int main()
{
scanf("%d%d%d",&n,&m,&g);
S = n + m + ;
T = n + m + ;
for (int i = ; i <= n; i++)
scanf("%d",&sex[i]);
for (int i = ; i <= n; i++)
scanf("%d",&v[i]);
for (int i = ; i <= n; i++)
{
if (sex[i] == )
add(S,i,v[i]);
else
add(i,T,v[i]);
}
for (int i = ; i <= m; i++)
{
int sexx,wi,num,flag;
scanf("%d%d%d",&sexx,&wi,&num);
ans += wi;
for (int j = ; j <= num; j++)
{
int temp;
scanf("%d",&temp);
if (sexx == )
add(temp,i + n,inf);
else
add(i + n,temp,inf);
}
scanf("%d",&flag);
if (flag)
wi += g;
if (sexx == )
add(i + n,T,wi);
else
add(S,i + n,wi);
}
dinic();
printf("%d\n",ans); return ;
}

 

Codeforces 311.E Biologist的更多相关文章

  1. CodeForces 311 B Cats Transport 斜率优化DP

    题目传送门 题意:现在有n座山峰,现在 i-1 与 i 座山峰有 di长的路,现在有m个宠物, 分别在hi座山峰,第ti秒之后可以被带走,现在有p个人,每个人会从1号山峰走到n号山峰,速度1m/s.现 ...

  2. 【CodeForces】【311E】Biologist

    网络流/最大权闭合图 题目:http://codeforces.com/problemset/problem/311/E 嗯这是最大权闭合图中很棒的一道题了- 能够1A真是开心-也是我A掉的第一道E题 ...

  3. Codeforces Round #311 (Div. 2) E. Ann and Half-Palindrome 字典树/半回文串

    E. Ann and Half-Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...

  4. Codeforces Round #311 (Div. 2) D. Vitaly and Cycle 图论

    D. Vitaly and Cycle Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/p ...

  5. Codeforces Round #311 (Div. 2) C. Arthur and Table Multiset

    C. Arthur and Table Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/p ...

  6. Codeforces Round #311 (Div. 2)B. Pasha and Tea 水题

    B. Pasha and Tea Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/prob ...

  7. Codeforces Round #311 (Div. 2) A. Ilya and Diplomas 水题

    A. Ilya and Diplomas Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/ ...

  8. Codeforces Round #311 (Div. 2) E - Ann and Half-Palindrome(字典树+dp)

    E. Ann and Half-Palindrome time limit per test 1.5 seconds memory limit per test 512 megabytes input ...

  9. Codeforces 311E Biologist

    Discription SmallR is a biologist. Her latest research finding is how to change the sex of dogs. In ...

随机推荐

  1. Vue 编程之路(一)——父子组件之间的数据传递

    最近公司的一个项目中使用 Vue 2.0 + element UI 实现一个后台管理系统的前端部分,属于商城类型.其中部分页面是数据管理页,所以有很多可以复用的表格,故引入自定义组件.在这里分享一下开 ...

  2. 3星|李开复《AI·未来》:中国创业公司有独特优势,人工智能可能会加剧社会的不平等与不稳定

    主要内容:作者对自己一些经历的回顾,对中美两国人工智能行业的回顾与展望. 作者认为中国的创业公司比美国节奏更快工作更拼命,深圳在硬件创新上远远领先于美国,中国创业公司们走出了一条跟美国不同的路. 作者 ...

  3. Java基础知识:Java实现Map集合二级联动4

    comboBox.setModel(new DefaultComboBoxModel(getProvince())); // 添加省份信息 final JLabel label = new JLabe ...

  4. JavaScript学习笔记(四)——DOM

    第五章 网页交互——文本对象模型[Document object model] 1 简单介绍DOM,dom是将html与javascript进行交互的工具. [使用innerHTML时注意:html中 ...

  5. fail-fast 机制 思考

    HashMap的迭代器(Iterator)是fail-fast迭代器,而Hashtable的enumerator迭代器不是fail-fast的. Iterator支持fail-fast机制,而Enum ...

  6. OA系统与Exchange 日历打通

    目前我碰到好几个案例是希望将客户以后的OA系统与Exchange中的日历系统相结合,比如致远或者泛微的OA系统. 客户的需求如下: 1.有了OA系统 2.客户使用Outlook当邮件客户端 3.客户希 ...

  7. UVA 11542 高斯消元

    从数组中选择几个数,要求他们的乘积可以开平方,问有多少种方案. 先将单个数拆分成质因子,对于这个数而言,那些指数为奇数的质因子会使这个数无法被开平方. 所以我们需要选择一个对应质因子指数为奇数的元素, ...

  8. 239. [LeetCode ]Sliding Window Maximum

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  9. zabbix监控历史数据清理

    2018/12/24 14:00:57  zabbix监控运行一段时间以后,会留下大量的历史监控数据,zabbix数据库一直在增大:可能会造成系统性能下降,查看历史数据室查询速度缓慢. zabbix里 ...

  10. log4j 配置使用

    使用log4j来管理日志信息,非常方便,下面简单介绍一下整个使用流程: 1.创建简单java项目 2.在类路径下新建log4j.properties文件 3.配置log4j.properties文件 ...