A simple brute force problem.
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 635 Accepted Submission(s): 335
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.
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题意:n个项目m个问题,完成每个项目有对应收入,解决每个问题需要对应花费,
给出每个项目需解决的问题以及各问题间的依赖关系,求最大利润(可完成部分或全部项目);
这是一个典型的最大权闭合图问题;
最大权闭合图:
将左图变成右图,s->t的最大流量= 由s流出的流量和 - (s->t)的最大流;
本题目中利益即为正权,花费即为负权,按照右图建边,彼此之间依赖,建边为无穷,用dinic算法或者sap算法都可以得到解决;
ac代码:
#include <iostream> #include <vector> #include <string.h> #include <queue> #include <stdio.h> using namespace std; const int maxn=1000,inf=0x3f3f3f3f; struct edge{ int to,cap,rev; edge(){;} edge(int a,int b,int c) { to=a,cap=b,rev=c; } }; vector<edge>G[maxn]; void init(int n) { for(int i=0;i<=n;i++) G[i].clear(); } int level[maxn],iter[maxn]; void addedge(int u,int v,int w) { G[u].push_back(edge(v,w,G[v].size())); G[v].push_back(edge(u,0,G[u].size()-1)); } void bfs(int s) { memset(level,-1,sizeof(level)); queue<int>que; level[s]=0; que.push(s); while(!que.empty()) { int u=que.front(); que.pop(); for(int i=0;i<G[u].size();i++) { int v=G[u][i].to; edge &e=G[u][i]; if(e.cap>0&&level[v]<0) { level[v]=level[u]+1; que.push(v); } } } } int dfs(int v,int t,int f) { if(v==t) return f; for(int &i=iter[v];i<G[v].size();i++) { edge &e=G[v][i]; if(e.cap>0&&level[e.to]>level[v]) { int d=dfs(e.to,t,min(f,e.cap)); if(d>0) { e.cap-=d; G[e.to][e.rev].cap+=d; return d; } } } return 0; } int dinic(int s,int t) { int flow=0; while(1) { bfs(s); if(level[t]<0)return flow; memset(iter,0,sizeof(iter)); int f; while((f=dfs(s,t,inf))>0) flow+=f; } return flow; } int main() { int n,m,sumf; int S,T; int t,TT=0; int c,k; cin>>t; while(t--) { scanf("%d%d",&n,&m); S=0,T=n+m+1; init(T); sumf=0; for(int i=1; i<=n; i++) { scanf("%d",&c); addedge(S,i,c); sumf+=c; } for(int i=1;i<=m;i++) { scanf("%d",&c); addedge(i+n,T,c); } for(int i=1;i<=n;i++) { scanf("%d",&k); for(int j=0;j<k;j++) { scanf("%d",&c); c++; addedge(i,c+n,inf); } } for(int i=1;i<=m;i++) { for(int j=1;j<=m;j++) { scanf("%d",&c); if(c) addedge(i+n,j+n,inf); } } printf("Case #%d: ",++TT); printf("%d\n",sumf-dinic(S,T)); } return 0; }