HDU 4971 A simple brute force problem.
2015-05-02 09:56
357 查看
A simple brute force problem.
Time Limit: 1000msMemory 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
2014 Multi-University Training Contest 10解题:网上说最大权闭合子图。。。可是我看 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 = 100; const int INF = 0x3f3f3f3f; struct arc{ int to,flow,next; arc(int x = 0,int y = 0,int z = -1){ 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,0,head[v]); head[v] = tot++; } bool bfs(){ queue<int>q; memset(d,-1,sizeof d); d[S] = 0; 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] == -1){ d[e[i].to] = d[u] + 1; q.push(e[i].to); } } } return d[T] > -1; } int dfs(int u,int low){ if(u == T) return low; int tmp = 0,a; for(int &i = cur[u]; ~i; i = e[i].next){ if(e[i].flow && d[e[i].to] == d[u]+1&&(a=dfs(e[i].to,min(e[i].flow,low)))){ e[i].flow -= a; low -= a; e[i^1].flow += a; tmp += a; if(!low) break; } } if(!tmp) d[u] = -1; return tmp; } int dinic(){ int ret = 0; 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 = 1; scanf("%d",&Ts); while(Ts--){ memset(head,-1,sizeof head); scanf("%d %d",&n,&m); tot = ret = S = 0; T = n + m + 1; for(int i = 1; i <= n; ++i){ scanf("%d",&w); add(S,i,w); ret += w; } for(int i = 1; i <= m; ++i){ scanf("%d",&w); add(i+n,T,w); } for(int i = 1; i <= n; ++i){ scanf("%d",&k); while(k--){ scanf("%d",&u); add(i,u + n + 1,INF); } } for(int i = 1; i <= m; ++i) for(int j = 1; j <= m; ++j){ scanf("%d",&w); if(w) add(i+n,j+n,INF); } printf("Case #%d: %d\n",cs++,ret - dinic()); } return 0; }
View Code
相关文章推荐
- hdu 4971 A simple brute force problem.【最小权闭包】
- HDU 4971 A simple brute force problem.(最小割,最大权闭合图)
- HDU 4971 A simple brute force problem 最大权闭合图
- HDU 4971 A simple brute force problem.(最大权闭合图)
- HDU 4971 A simple brute force problem.(dp)
- HDU - 4971 A simple brute force problem. (DP)
- hdu 多校联合 4971 A simple brute force problem.(最大权闭合图)
- HDU 4971 - A simple brute force problem【最大权闭合图】
- 【最小割】HDU 4971 A simple brute force problem.
- HDU 4971 A simple brute force problem. 最小割 最大权闭合
- HDU 4971 A simple brute force problem.(最大流)
- 【HDU】4971 A simple brute force problem. 强连通缩点+最大权闭合子图
- hdu - 4971 - A simple brute force problem.(最大权闭合图)
- hdu - 4971 - A simple brute force problem.(最大权闭合图)
- HDU-4971-A simple brute force problem.
- HDU 4971 A simple brute force problem.(最大权闭合)
- HDU 4971 A simple brute force problem
- HDU 4971 A simple brute force problem.
- 【最小割】HDU 4971 A simple brute force problem.
- HDU 4971 A simple brute force problem. 强连通缩点+最大权闭合图