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. Android configChanges使用方法

    1.    在manifest文件里使用activity的默认属性.横屏竖屏时,惠重复调用onDestory和onCreate  造成不必要的开销.Android默认如此应该是为了适配不同的xml布局 ...

  2. vim 插件之vundle

    vundle这个插件主要是用来插件管理的.它可以根据你配置的插件地址,自动下载.更新.删除插件,非常的好用 地址 https://github.com/gmarik/vundle 然后你在 .vim下 ...

  3. github如何多人开发一个项目

    github如何多人开发一个项目 一.总结 一句话总结:a.点项目里面的Settings->Collaborators,来添加参与者(比如github用户名), b.向他发送项目的link,让他 ...

  4. 2.mongoDB 介绍(特点、优点、原理)

    转自:https://www.cnblogs.com/hoojo/archive/2011/06/01/2066119.html 介绍:MongoDB是一个基于分布式文件存储的数据库.由C++语言编写 ...

  5. Dictionary subtraction

    Finding the words from the book that are not in the word list from words.txt is a problem you might ...

  6. Deleting elements

    There are several ways to delete elements from a list. If you know the index of the element you want ...

  7. 使用Visual Studio2012调试Redis源码

    Redis是一款C语言编写Key-Value存储系统,基于BSD协议开放源码,其源码托管在github上,大概有三万行. 源码地址:https://github.com/antirez/redis 源 ...

  8. php 自带加密函数 mcrypt_encrypt

    <?php /** * AES128加解密类 * @author dy * */ class Aes{ //密钥 private $_secrect_key; public function _ ...

  9. [转] -- html5手机网站自适应需要加的meta标签

    webapp开发初期,会碰到在pc端开发好的页面在移动端显示过大的问题,这里需要在html head中加入meta标签来控制缩放 <meta name=" viewport" ...

  10. Linux服务器性能评估与优化

    一.影响务器性能因素 影响企业生产环境Linux服务器性能的因素有很多,一般分为两大类,分别为操作系统层级和应用程序级别.如下为各级别影响性能的具体项及性能评估的标准: (1)操作系统级别 内存: C ...