A simple brute force problem.

Time Limit: 1000ms
Memory Limit: 65536KB

This problem will be judged on HDU. Original ID: 4971
64-bit integer IO format: %I64d      Java class name: Main

There's a company with several projects to be done. Finish a project will get you profits. However, there are some technical problems for some specific projects. To solve the problem, the manager will train his employee which may cost his budget. There may be dependencies between technical problems, for example, A requires B means you need to solve problem B before solving problem A. If A requires B and B requires A, it means that you should solve them at the same time. You can select which problems to be solved and how to solve them freely before finish your projects. Can you tell me the maximum profit?

 

Input

The first line of the input is a single integer T(<=100) which is the number of test cases.

Each test case contains a line with two integer n(<=20) and m(<=50) which is the number of project to select to complete and the number of technical problem.

Then a line with n integers. The i-th integer(<=1000) means the profit of complete the i-th project.

Then a line with m integers. The i-th integer(<=1000) means the cost of training to solve the i-th technical problem.

Then n lines. Each line contains some integers. The first integer k is the number of technical problems, followed by k integers implying the technical problems need to solve for the i-th project.

After that, there are m lines with each line contains m integers. If the i-th row of the j-th column is 1, it means that you need to solve the i-th problem before solve the j-th problem. Otherwise the i-th row of the j-th column is 0.

 

Output

For each test case, please output a line which is "Case #X: Y ", X means the number of the test case and Y means the the maximum profit.

 

Sample Input

4
2 3
10 10
6 6 6
2 0 1
2 1 2
0 1 0
1 0 0
0 0 0
2 3
10 10
8 10 6
1 0
1 2
0 1 0
1 0 0
0 0 0
2 3
10 10
8 10 6
1 0
1 2
0 1 0
0 0 0
0 0 0
2 3
10 10
8 10 6
1 0
1 2
0 0 0
1 0 0
0 0 0

Sample Output

Case #1: 2
Case #2: 4
Case #3: 4
Case #4: 6

Source

 
解题:网上说最大权闭合子图。。。可是我看 If the i-th row of the j-th column is 1, it means that you need to solve the i-th problem before solve the j-th problem
 
我觉得是j+n向i+n连边,因为选j必须先选i,由闭合子图的性质,j-i是j的出边,那么j-i必须选
 
可是不知为何是错的
 
 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f;
struct arc{
int to,flow,next;
arc(int x = ,int y = ,int z = -){
to = x;
flow = y;
next = z;
}
}e[maxn*maxn];
int head[maxn],d[maxn],cur[maxn],tot,S,T;
void add(int u,int v,int flow){
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,,head[v]);
head[v] = tot++;
}
bool bfs(){
queue<int>q;
memset(d,-,sizeof d);
d[S] = ;
q.push(S);
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].flow && d[e[i].to] == -){
d[e[i].to] = d[u] + ;
q.push(e[i].to);
}
}
}
return d[T] > -;
}
int dfs(int u,int low){
if(u == T) return low;
int tmp = ,a;
for(int &i = cur[u]; ~i; i = e[i].next){
if(e[i].flow && d[e[i].to] == d[u]+&&(a=dfs(e[i].to,min(e[i].flow,low)))){
e[i].flow -= a;
low -= a;
e[i^].flow += a;
tmp += a;
if(!low) break;
}
}
if(!tmp) d[u] = -;
return tmp;
}
int dinic(){
int ret = ;
while(bfs()){
memcpy(cur,head,sizeof head);
ret += dfs(S,INF);
}
return ret;
}
int main(){
int Ts,n,m,u,v,w,k,ret,cs = ;
scanf("%d",&Ts);
while(Ts--){
memset(head,-,sizeof head);
scanf("%d %d",&n,&m);
tot = ret = S = ;
T = n + m + ;
for(int i = ; i <= n; ++i){
scanf("%d",&w);
add(S,i,w);
ret += w;
}
for(int i = ; i <= m; ++i){
scanf("%d",&w);
add(i+n,T,w);
}
for(int i = ; i <= n; ++i){
scanf("%d",&k);
while(k--){
scanf("%d",&u);
add(i,u + n + ,INF);
}
}
for(int i = ; i <= m; ++i)
for(int j = ; j <= m; ++j){
scanf("%d",&w);
if(w) add(i+n,j+n,INF);
}
printf("Case #%d: %d\n",cs++,ret - dinic());
}
return ;
}

HDU 4971 A simple brute force problem.的更多相关文章

  1. hdu - 4971 - A simple brute force problem.(最大权闭合图)

    题意:n(n <= 20)个项目,m(m <= 50)个技术问题,做完一个项目能够有收益profit (<= 1000),做完一个项目必须解决对应的技术问题,解决一个技术问题须要付出 ...

  2. HDU 4971 - A simple brute force problem【最大权闭合图】

    有n(20)个工程,完成每个工程获得收益是p[i],m(50)个需要解决的难题,解决每个难题花费是c[i] 要完成第i个工程,需要先解决ki个问题,具体哪些问题,输入会给出 每个难题之间可能有依赖关系 ...

  3. 【最小割】HDU 4971 A simple brute force problem.

    说是最大权闭合图.... 比赛时没敢写.... 题意 一共同拥有n个任务,m个技术 完毕一个任务可盈利一些钱,学习一个技术要花费钱 完毕某个任务前须要先学习某几个技术 可是可能在学习一个任务前须要学习 ...

  4. HDU4971 A simple brute force problem.(强连通分量缩点 + 最大权闭合子图)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4971 Description There's a company with several ...

  5. A simple brute force problem.

    hdu4971:http://acm.hdu.edu.cn/showproblem.php?pid=4971 题意:给你n个项目,每完成一个项目会有一定的收益,但是为了完成某个项目,要先学会一些技能, ...

  6. hdu 4972 A simple dynamic programming problem(高效)

    pid=4972" target="_blank" style="">题目链接:hdu 4972 A simple dynamic progra ...

  7. HDU 4975 A simple Gaussian elimination problem.

    A simple Gaussian elimination problem. Time Limit: 1000ms Memory Limit: 65536KB This problem will be ...

  8. hdu 4975 A simple Gaussian elimination problem.(网络流,推断矩阵是否存在)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4975 Problem Description Dragon is studying math. One ...

  9. hdu - 4975 - A simple Gaussian elimination problem.(最大流量)

    意甲冠军:要在N好M行和列以及列的数字矩阵和,每个元件的尺寸不超过9,询问是否有这样的矩阵,是独一无二的N(1 ≤ N ≤ 500) , M(1 ≤ M ≤ 500). 主题链接:http://acm ...

随机推荐

  1. 2014 Unity3d大会的部分总结

    一.项目开发.管理和公布策略 1.       四大准则 a.       美术的资源量 b.       美术规范,要依据开发什么样的游戏制定统一的规范,这样尽可能的形成统一的规范.然后程序要协助美 ...

  2. C#重构经典全面汇总

    C#重构经典全面汇总 1.  封装集合 概念:本文所讲的封装集合就是把集合进行封装,仅仅提供调用端须要的接口. 正文:在非常多时候,我们都不希望把一些不必要的操作暴露给调用端,仅仅须要给它所须要的操作 ...

  3. Win 10最大的亮点不是免费而是人工智能

    7月27日,日本知名作家Manish Singh发表文章.题为"Eight Reasons Why You Should Upgrade to Windows 10",文中例举下面 ...

  4. hdu_1166,线段树单点更新

    在刷线段树,参考自http://www.notonlysuccess.com/index.php/segment-tree-complete/ #include<iostream> #in ...

  5. User-defined types

    We have used many of Python’s built-in types; now we are going to define a new type. As an example, ...

  6. iOS中关于字符 “&”的作用?

    如NSFileManager中关于判断是否目录的 iOS中关于字符 "&"的作用? >> ios这个答案描述的挺清楚的:http://www.goodpm.ne ...

  7. 关于网易云音乐爬虫的api接口?

    抓包能力有限,分析了一下网易云音乐的一些api接口,但是关于它很多post请求都是加了密,没有弄太明白.之前在知乎看到过一个豆瓣工程师写的教程,但是被投诉删掉了,请问有网友fork了的吗?因为我觉得他 ...

  8. jQuery新浪微博表情插件教程

    1.引入css文件 <link rel="stylesheet" type="text/css" href="jquery.sinaEmotio ...

  9. Linux 下易用的光盘镜像管理工具(虚拟光驱软件)转载

    作者: Frazer Kline | 2014-11-23 11:07   评论: 4 收藏: 4 分享: 10 磁盘镜像包括了整个磁盘卷的文件或者是全部的存储设备的数据,比如说硬盘,光盘(DVD,C ...

  10. python BeautifulSoup 获取页面多个子节点中的各个节点的内容

    页面html格式为 <tr bgcolor="#7bb5de"><td style="border-bottom: 1px solid #C9D8AD& ...