# HDU4971 A simple brute force problem.

17 篇文章 0 订阅
9 篇文章 0 订阅

## A simple brute force problem.

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 36 Accepted Submission(s): 10

Problem Description
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
2014 Multi-University Training Contest 10

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=110;
const int MAXE=6010;
const int INF=1<<30;
struct EDGE
{
int v,next;
int cap;
}edge[MAXE];
void init()
{
size=0;
}
{
edge[size].v=v;
edge[size].cap=cap;
edge[size].v=u;
edge[size].cap=0;
}
int pe[MAXN],pre[MAXN],gap[MAXN],dist[MAXN],nv;
int ISAP(int s,int t){
int cur_flow,u,temp,neck,i;
int max_flow;
memset(gap,0,sizeof(gap));
memset(pre,-1,sizeof(pre));
memset(dist,0,sizeof(dist));
gap[nv]=nv;
max_flow=0;
u=s;
while(dist[s]<nv){
if(u==t){
cur_flow=INF;
for(i=s;i!=t;i=edge[pe[i]].v){
if(cur_flow>edge[pe[i]].cap){
neck=i;
cur_flow=edge[pe[i]].cap;
}
}
for(i=s;i!=t;i=edge[pe[i]].v){
temp=pe[i];
edge[temp].cap-=cur_flow;
temp^=1;
edge[temp].cap+=cur_flow;
}
max_flow+=cur_flow;
u=neck;
}
for(i=pe[u];i!=-1;i=edge[i].next)
if(edge[i].cap>0&&dist[u]==dist[edge[i].v]+1)
break;
if(i!=-1){
pe[u]=i;
pre[edge[i].v]=u;
u=edge[i].v;
}
else{
if(0==--gap[dist[u]]) break;
if(edge[i].cap > 0)
temp=temp<dist[edge[i].v]?temp:dist[edge[i].v];
dist[u]=temp+1;
++gap[dist[u]];
if(u!=s)u=pre[u];
}
}
return max_flow;
}
int main()
{
int T,s,t,n,m,i,j,x,flag=1;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
init();
s=0,t=n+m+1;
nv=t+1;
int sum=0;
for(i=1;i<=n;i++)
{
scanf("%d",&x);
sum+=x;
}
for(i=1;i<=m;i++)
{
scanf("%d",&x);
}
int k;
for(i=1;i<=n;i++)
{
scanf("%d",&k);
while(k--)
{
scanf("%d",&x);
x++;
}
}
for(i=1;i<=m;i++)
{
for(j=1;j<=m;j++)
{
scanf("%d",&x);
if(x==1)
{
}
}
}
int ans=ISAP(s,t);
printf("Case #%d: %d\n",flag++,sum-ans);
}
return 0;
}


• 0
点赞
• 0
收藏
觉得还不错? 一键收藏
• 2
评论
08-27 383
04-22 409
10-02 476
08-24 497
09-23
08-13 834
07-25 546
08-07 543
07-23 486
07-29 485
08-06 448

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。