Matrix
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 823 Accepted Submission(s): 210
Problem Description
Let A be a 1*N matrix, and each element of A is either 0 or 1. You are to find such A that maximize D=(A*B-C)*AT, where B is a given N*N matrix whose elements are non-negative, C is a given 1*N matrix whose elements are also non-negative, and AT is the transposition of A (i.e. a N*1 matrix).
Input
The first line contains the number of test cases T, followed by T test cases.
For each case, the first line contains an integer N (1<=N<=1000).
The next N lines, each of which contains N integers, illustrating the matrix B. The jth integer on the ith line is B[i][j].
Then one line followed, containing N integers, describing the matrix C, the ith one for C[i].
You may assume that sum{B[i][j]} < 2^31, and sum{C[i]} < 2^31.
For each case, the first line contains an integer N (1<=N<=1000).
The next N lines, each of which contains N integers, illustrating the matrix B. The jth integer on the ith line is B[i][j].
Then one line followed, containing N integers, describing the matrix C, the ith one for C[i].
You may assume that sum{B[i][j]} < 2^31, and sum{C[i]} < 2^31.
Output
For each case, output the the maximum D you may get.
Sample Input
1 3 1 2 1 3 1 0 1 2 3 2 3 7
Sample Output
2HintFor sample, A=[1, 1, 0] or A=[1, 1, 1] would get the maximum D.
Author
BUPT
Source
Recommend
zhuyuanchen520
网上看的题解,一看是矩阵就不是很想碰的题。看过题解才知道,最小割的应用还是很强的。
思路:

如果Ai选择0会产生sum{Bij}(1<=j<=N)的代价,如果Ai选择1会产生Ci的代价,如果Ai选择1且aj选择0就会产生Bij的代价。这样就可以建模了。
设源点和汇点,如果Ai选0则从源点到i连接一条容量为sum(Bij)的边,如果Ai选1则从i到汇点连接一条容量为Ci的边,对于从i到j的边,即Ai选了1,并且Aj选了0的边,其容量就为Bij。则从源点到汇点的最大流即最小割,那么D最大即sum(Bij)(1<=i<=N,1<=j<=N)-最小割。
#include<cstdio>
using namespace std;
const int mm=2005005;
const int mn=1005;
const int oo=1000000000;
int node,s,t,edge;
int to[mm],flow[mm],next[mm];
int head[mn],work[mn],dis[mn],q[mn];
inline int min(int a,int b)
{
return a<b?a:b;
}
inline void init(int nn,int ss,int tt)
{
node=nn,s=ss,t=tt,edge=0;
for(int i=0; i<node; ++i) head[i]=-1;
}
inline void add(int u,int v,int c1,int c2=0)
{
to[edge]=v,flow[edge]=c1,next[edge]=head[u],head[u]=edge++;
to[edge]=u,flow[edge]=c2,next[edge]=head[v],head[v]=edge++;
}
bool bfs()
{
int i,u,v,l,r=0;
for(i=0; i<node; ++i) dis[i]=-1;
dis[q[r++]=s]=0;
for(l=0; l<r; ++l)
for(i=head[u=q[l]]; i>=0; i=next[i])
if(flow[i]&&dis[v=to[i]]<0)
{
dis[q[r++]=v]=dis[u]+1;
if(v==t)return 1;
}
return 0;
}
int dfs(int u,int maxf)
{
if(u==t) return maxf;
for(int &i=work[u],v,tmp; i>=0; i=next[i])
if(flow[i]&&dis[v=to[i]]==dis[u]+1&&(tmp=dfs(v,min(maxf,flow[i])))>0)
{
flow[i]-=tmp;
flow[i^1]+=tmp;
return tmp;
}
return 0;
}
long long dinic()
{
int i,delta;
long long ret=0;
while(bfs())
{
for(i=0; i<node; ++i) work[i]=head[i];
while(delta=dfs(s,oo))ret+=delta;
}
return ret;
}
int main()
{
int i,j,n,a,T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
init(n+2,0,n+1);
long long fcnt=0;
for(i=1; i<=n; ++i)
{
int ss=0;
for(j=1; j<=n; ++j)
{
scanf("%d",&a),ss+=a;
add(i,j,a);
}
add(s,i,ss);
fcnt+=ss;
}
for(int i=1; i<=n; i++)
{
scanf("%d",&a);
add(i,t,a);
}
printf("%I64d\n",fcnt-dinic());
}
return 0;
}