A simple brute force problem.
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 186 Accepted Submission(s): 119
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种技术,完成某些工程可能需要先学习某些技术,完成一个工程可以得到该工程的收益,学习一种技术也要付出一定的花费,求最大收益,并且某种技
术的学习可能需要先学另一种技术,并且可以相互需要,如果是相互需要则两种技术要同时学习
思路:由于技术与技术之间可能相互都有边,所以就可能存在环,可以先用强连通分量缩点,然后就可以当做DAG图来处理了,最后就可以转换为网络流模型,首先源点与
各个工程之间连边,流量为该工程的收益,然后各个技术与汇点连边,流量为学习该技术的花费,然后是技术与技术之间,如果学习技术 i 要先学习技术 j,则从 i 向 j 连边,流
量为正无穷,最后是工程与技术之间,若工程 i 需要技术 j ,则从 i 想 j 连边,流量为该工程的收益,跑一遍最大流,答案为所有工程的总收益减最大流
#include
#include
#include
#include
#include
using namespace std; #define INF ((1<<31)-1) struct node{ int u; int v; int cf; int f; }; int n,m; int a[22][55]; int num2[22]; int por[22]; int cost[55]; int head[55]; int edge[300]; int next[300]; int d,d1; int ans; int dfs_clock,scc_cnt; int sccno[55]; int pre[55]; int lowlink[55]; int flow[55]; int vis1[55][55]; int head1[80]; int vis[55]; node edge1[300<<1]; int next1[300<<1]; int num[80]; int level[80]; int cur[80]; int p[80]; stack
S; void add(int u,int v) { edge[d]=v; next[d]=head[u]; head[u]=d++; } int mini(int x,int y) { return x
edge1[i].cf){ ok=1; p[edge1[i].v]=i; cur[x]=i; x=edge1[i].v; break; } if(!ok){ if(--num[level[x]]==0)break; level[x]=vn; for(i=head1[x];i!=-1;i=next1[i])if(edge1[i].f>edge1[i].cf&&level[edge1[i].v]+1