HDU 4971 A simple brute force problem.

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必须选
 
可是不知为何是错的
 
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 100;
 4 const int INF = 0x3f3f3f3f;
 5 struct arc{
 6     int to,flow,next;
 7     arc(int x = 0,int y = 0,int z = -1){
 8         to = x;
 9         flow = y;
10         next = z;
11     }
12 }e[maxn*maxn];
13 int head[maxn],d[maxn],cur[maxn],tot,S,T;
14 void add(int u,int v,int flow){
15     e[tot] = arc(v,flow,head[u]);
16     head[u] = tot++;
17     e[tot] = arc(u,0,head[v]);
18     head[v] = tot++;
19 }
20 bool bfs(){
21     queue<int>q;
22     memset(d,-1,sizeof d);
23     d[S] = 0;
24     q.push(S);
25     while(!q.empty()){
26         int u = q.front();
27         q.pop();
28         for(int i = head[u]; ~i; i = e[i].next){
29             if(e[i].flow && d[e[i].to] == -1){
30                 d[e[i].to] = d[u] + 1;
31                 q.push(e[i].to);
32             }
33         }
34     }
35     return d[T] > -1;
36 }
37 int dfs(int u,int low){
38     if(u == T) return low;
39     int tmp = 0,a;
40     for(int &i = cur[u]; ~i; i = e[i].next){
41         if(e[i].flow && d[e[i].to] == d[u]+1&&(a=dfs(e[i].to,min(e[i].flow,low)))){
42             e[i].flow -= a;
43             low -= a;
44             e[i^1].flow += a;
45             tmp += a;
46             if(!low) break;
47         }
48     }
49     if(!tmp) d[u] = -1;
50     return tmp;
51 }
52 int dinic(){
53     int ret = 0;
54     while(bfs()){
55         memcpy(cur,head,sizeof head);
56         ret += dfs(S,INF);
57     }
58     return ret;
59 }
60 int main(){
61     int Ts,n,m,u,v,w,k,ret,cs = 1;
62     scanf("%d",&Ts);
63     while(Ts--){
64         memset(head,-1,sizeof head);
65         scanf("%d %d",&n,&m);
66         tot = ret = S = 0;
67         T = n + m + 1;
68         for(int i = 1; i <= n; ++i){
69             scanf("%d",&w);
70             add(S,i,w);
71             ret += w;
72         }
73         for(int i = 1; i <= m; ++i){
74             scanf("%d",&w);
75             add(i+n,T,w);
76         }
77         for(int i = 1; i <= n; ++i){
78             scanf("%d",&k);
79             while(k--){
80                 scanf("%d",&u);
81                 add(i,u + n + 1,INF);
82             }
83         }
84         for(int i = 1; i <= m; ++i)
85         for(int j = 1; j <= m; ++j){
86             scanf("%d",&w);
87             if(w) add(i+n,j+n,INF);
88         }
89         printf("Case #%d: %d
",cs++,ret - dinic());
90     }
91     return 0;
92 }
View Code
原文地址:https://www.cnblogs.com/crackpotisback/p/4471582.html