A simple brute force problem.
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 330 Accepted Submission(s): 185
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
Source
易证 权闭合图 的花费
收益 = (所有收益) - (未选收益 + 已选花费)
后部分 即为 图上 最小割 , 即最大流
#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
struct Edge
{
int from, to , cap , next ;
Edge( int from = 0 , int to = 0 , int cap = 0, int next=0):
from(from),to(to),cap(cap),next(next){};
}e[100 * 100 *2];
const int inf = 0x3f3f3f3f;
int n,m,cnt,idx,tot;
int dfn[100],low[100],head[100],dis[100],cur[100],scc[100];
int beni[100],cost[100],val[100];
vector <int> g[100];
vector <int> mp[100];
bool map[100][100];
stack <int> s;
void add(int from,int to, int cap)
{
e[tot]=Edge(from,to,cap,head[from]);
head[from]=tot++;
e[tot]=Edge(to,from,0, head[to]);
head[to]=tot++;
}
bool bfs(int S,int T)
{
memset(dis,-1,sizeof(dis));
queue<int> q;
dis[S] = 0;
q.push(S);
while (!q.empty())
{
int u = q.front(); q.pop();
for (int i=head[u]; i!=-1; i=e[i].next)
{
int v= e[i].to;
if (dis[v] == -1 && e[i].cap)
{
dis[v]= dis[u] + 1;
q.push( v );
if (dis[T]!= -1 ) return true;
}
}
}
return false;
}
int dfs(int u , int a, int T)
{
if ( u==T|| a==0 ) return a;
int flow = 0, f;
for (int &i=cur[u]; i!=-1; i=e[i].next)
{
int v=e[i].to;
if (e[i].cap && dis[u]+1==dis[v] && (f=dfs( v, min(e[i].cap, a), T) ) )
{
e[i].cap -= f;
e[i^1].cap +=f;
flow += f; a -=f ;
if ( a==0 ) return flow;
}
}
return flow;
}
int maxflow(int S, int T)
{
int flow = 0;
while ( bfs( S, T) )
{
memcpy( cur, head, sizeof(cur) );
flow += dfs( S, inf ,T);
}
return flow;
}
void tarjan(int u)
{
dfn[u] = low[u] = ++idx;
s.push(u);
for (int i=0; i<m; i++)
if ( map[u][i] )
{
int v= i;
if (!dfn[v])
{
tarjan( v );
low[u] = min(low[u] ,low[v]);
}
else
if (scc[v]==-1)
low[u] = min( low[u], dfn[v]);
}
if ( low[u] == dfn[u])
{
while (1)
{
int x = s.top();
scc[x] = cnt;
val[cnt] += cost[x];
s.pop();
if (x==u) break;
}
cnt ++;
}
}
int main()
{
int TT,cas=0;
scanf("%d",&TT);
while (TT--)
{
int sum = 0;
scanf("%d%d",&n,&m);
for (int i=0; i<n; i++)
{
scanf("%d",&beni[i]);
sum += beni[i];
}
for (int i=0; i<m; i++)
scanf("%d",&cost[i]);
for (int i=0; i<n; i++)
{
int k,x;
g[i].clear();
scanf("%d", &k);
for (int j= 0; j<k; j++)
{
scanf("%d",&x);
g[i].push_back( x );
}
}
for (int i=0; i<m; i++)
for (int j=0; j<m; j++)
scanf("%d",&map[i][j]);
tot = cnt = idx = 0;
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(scc,-1,sizeof(scc));
memset(val,0,sizeof(val));
memset(head,-1,sizeof(head));
for (int i=0; i<m; i++)
if (!dfn[i]) tarjan(i);
for (int i=0; i<n+cnt; i++)
mp[i].clear();
for (int i=0; i<n; i++)
{
for (int j=0; j<g[i].size(); j++)
mp[i].push_back( n+scc[g[i][j]] );
}
for (int i=0; i<m; i++)
for (int j=0; j<m; j++)
if ( map[i][j] && scc[i]!=scc[j])
mp[ n +scc[i] ].push_back( n+ scc[j] );
int S = n+cnt, T = n+cnt +1;
for (int i=0; i<n; i++)
add( S, i , beni[i]);
for (int i=0; i<n+cnt; i++)
for (int j= 0; j<mp[i].size(); j++)
add( i, mp[i][j] , inf);
for (int i=0; i<cnt; i++)
add( i+n ,T , val[i]);
int ret = sum - maxflow (S, T);
printf("Case #%d: %d\n",++cas, ret);
}
return 0;
}