hdu 4307 Matrix(网络流,dinic,4级)

Matrix

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 829    Accepted Submission(s): 213


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.
 

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
2
Hint
For sample, A=[1, 1, 0] or A=[1, 1, 1] would get the maximum D.
 

Author
BUPT
 

Source
 

Recommend
zhuyuanchen520

思路:

之所以能够用最大流解决这个问题,关键在于最大流可以求解下面这个函数的最小值:

接下来就分析一下如何用最大流求解上面这个函数的极值。

首先xi一共只有两种选择,那么最终可以按xi的取值将xi划分成两个集合,那么如果xi在值为1的集合里,xj在值为0的集合里,那么就会

产生一个代价cij。同时如果xi选择0就会产生一个bi的代价,如果xi选择1就会产生一个ai的代价。于是构造一个源点S,汇点T做最小

割,不妨假设做完最小割之后值为1的xi的集合是和S相连的部分,值为0的xi的集合是和T相连的部分。

 

由于表达式中有三项,我们用三种割边来分别描述这三项的值。一种是xi选择了1,这样就不能选择0,需要把xi-T这条边割掉,由于xi

选择1会产生ai的代价,那么就把这条边的容量设为ai。另一种是xi选择了0,这样就不能选择1,需要把S-xi这条边割掉,由于xi选择0会

产生bi的代价,那么就把这条边的容量设为bi。最后一种是xi选择了1,xj选择了0,这样xi和xj不能在同一个集合中,需要把xi-xj这条边割

,由于xi选择1,xj选择0产生cij的代价,那么就把这条边的容量设为cij。这样对建好的图做最小割就可以得到上面哪个函数的最小

值。

 

接着我们分析这个题目如何转化成上面这种模型。首先我们将D的表达式赤裸裸地写出来:


这种形式必然不能看出来和上面那个表达式有什么关系,于是我们继续将其化简:

如果令f等于最后一行括号里的内容,那么发生了什么?如果ai选择0会产生sum{bij}(1<=j<=N)的代价,如果ai选择1会产生ci的代价,如

果ai选择1且aj选择0就会产生bij的代价。这样就完全转化成了上面的模型。

以上题解,摘自ACdream

本题难在建图。


#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int mm=4e6+9;
const int mn=4e3+9;
const __int64 oo=1e18;
class knode
{
 public:int v,next;__int64 flow;
}e[mm];
int src,dest,edge,node,n;
int head[mn],work[mn],dis[mn],q[mn];
void data(int _node,int _src,int _dest)
{
 node=_node;src=_src;dest=_dest;
 memset(head,-1,sizeof(head));
 edge=0;
}
void add(int u,int v,int c)
{
 e[edge].v=v;e[edge].flow=c;e[edge].next=head[u];head[u]=edge++;
 e[edge].v=u;e[edge].flow=0;e[edge].next=head[v];head[v]=edge++;
}
bool bfs()
{
  int l=0,r=1,u,v;
  for(int i=1;i<=node;++i)
  dis[i]=-1;
  q[l]=src;dis[src]=0;
  while(l^r)
  {
   u=q[l++];l%=mn;
   for(int i=head[u];~i;i=e[i].next)
   { v=e[i].v;
     if(e[i].flow&&dis[v]==-1)
     {
      dis[v]=dis[u]+1;q[r++]=v;r%=mn;
      if(v==dest)return 1;
     }
   }
  }
  return 0;
}
__int64 dfs(int u,__int64 exp)
{
 if(u==dest)return exp;
 int v;__int64 tmp;
 for(int&i=work[u];~i;i=e[i].next)
 { v=e[i].v;
  if(e[i].flow&&dis[v]==dis[u]+1&&(tmp=dfs(v,min(exp,e[i].flow)))>0)
  {
   e[i].flow-=tmp;e[i^1].flow+=tmp;return tmp;
  }
 }
 return 0;
}
__int64 dinic_flow()
{
 __int64 sum=0,data;
 while(bfs())
 {
  for(int i=0;i<node;++i)work[i]=head[i];
  while(data=dfs(src,oo))sum+=data;
 }
 return sum;
}
int main()
{__int64 sum;
 int cas,a,x;
 while(~scanf("%d",&cas))
 {
  while(cas--)
  { sum=0;
   scanf("%d",&n);
   data(n+2,0,n+1);
   for(int i=1;i<=n;++i)
   {
    a=0;
    for(int j=1;j<=n;++j)
    {
     scanf("%d",&x);a+=x;
     add(i,j,x);
    }
    sum+=a;
    add(src,i,a);
   }
   for(int i=1;i<=n;++i)
   {
    scanf("%d",&x);
    add(i,dest,x);
   }
   printf("%I64d\n",sum-dinic_flow());
  }
 }
 return 0;
}






posted @ 2013-05-30 11:34  剑不飞  阅读(233)  评论(0编辑  收藏  举报