bzoj 2406: 矩阵 上下界网络流判定

时间:2021-07-31 15:27:49

2406: 矩阵

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 138  Solved: 46
[Submit][Status][Discuss]

Description

 

Input

第一行两个数n、m,表示矩阵的大小。

接下来n行,每行m列,描述矩阵A。

最后一行两个数L,R。

Output

第一行,输出最小的答案;

Sample Input

2 2

0 1

2 1

0 1

Sample Output

1

HINT

对于100%的数据满足N,M<=200,0<=L<=R<=1000,0<=Aij<=1000

  看网上没什么题解,就随便写一写吧,二分答案转化为判定问题:构造矩阵,使得每行每列之和分别满足在一个区间内,这就是很裸很裸的带下界网络流判定问题。直接行列分别建点即可,然而我写的时候数组开小了,wa了一个上午。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 220
#define MAXV MAXN*MAXN
#define MAXE MAXV*2
#define INF 0x3f3f3f3f
#define BIG 100000000
#define abs(x) ((x)<0?(-(x)):(x))
struct Edge
{
int val,np;
Edge *next,*neg;
}E[MAXE],*V[MAXV];
int tope=;
int sour=,sink=;
inline void addedge(int x,int y,int z)
{
// cout<<"Add edge:<"<<tope+1<<">"<<x<<" "<<y<<":"<<z<<endl;
E[++tope].np=y;
E[tope].val=z;
E[tope].next=V[x];
V[x]=&E[tope]; E[++tope].np=x;
E[tope].val=;
E[tope].next=V[y];
V[y]=&E[tope]; E[tope].neg=&E[tope-];
E[tope-].neg=&E[tope];
}
int q[MAXV],lev[MAXV];
int vis[MAXV],bfstime=;
bool bfs()
{
int head=-,tail=;
Edge *ne;
lev[sour]=;
vis[sour]=++bfstime;
q[]=sour;
while (head<tail)
{
for (ne=V[q[++head]];ne;ne=ne->next)
{
if (!ne->val || vis[ne->np]==bfstime)continue;
q[++tail]=ne->np;
vis[ne->np]=bfstime;
lev[ne->np]=lev[q[head]]+;
}
}
return vis[sink]==bfstime;
}
int dfs(int now,int maxf)
{
int ret=,t;
if (now==sink || !maxf)return maxf;
Edge* ne;
for (ne=V[now];ne;ne=ne->next)
{
if (!ne->val || lev[ne->np]!=lev[now]+)continue;
t=dfs(ne->np,min(maxf,ne->val));
ne->val-=t;
ne->neg->val+=t;
maxf-=t;
ret+=t;
//cout<<"Flow:"<<now<<"-"<<ne->np<<":"<<x<<"("<<ne->val<<")"<<endl;
}
if (!ret)lev[now]=-;
return ret;
}
int dinic()
{
int ret=;
while (bfs())
{
ret+=dfs(sour,INF);
}
return ret;
} int mat[MAXN][MAXN];
int degin[MAXV],degout[MAXV];
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int n,m,a,b;
scanf("%d%d",&n,&m);
for (int i=;i<n;i++)
for (int j=;j<m;j++)
{
scanf("%d",&mat[i][j]);
}
scanf("%d%d",&a,&b);
int l=-,r=;
int tsour=,tsink=;
while (l+<r)
{
memset(degin,,sizeof(degin));
memset(degout,,sizeof(degout));
memset(V,,sizeof(V));
tope=-;
int mid=(l+r)>>;
for (int i=;i<n;i++)
{
int s=;
for (int j=;j<m;j++)
s+=mat[i][j];
degout[tsour]+=s-mid;
degin[+i]+=s-mid;
addedge(tsour,+i,mid*);
}
for (int i=;i<m;i++)
{
int s=;
for (int j=;j<n;j++)
s+=mat[j][i];
degin[tsink]+=s-mid;
degout[+i+n]+=s-mid;
addedge(+i+n,tsink,mid*);
}
for (int i=;i<n;i++)
{
for (int j=;j<m;j++)
{
degout[+i]+=a;
degin[+j+n]+=a;
addedge(+i,+j+n,b-a);
}
}
addedge(tsink,tsour,BIG);
int sum=;
for (int i=;i<+n+m;i++)
{
if (degout[i]>degin[i])
{
sum+=degout[i]-degin[i];
addedge(i,sink,degout[i]-degin[i]);
}
else if (degin[i]>degout[i])
addedge(sour,i,degin[i]-degout[i]);
}
if (dinic()==sum)
r=mid;
else
l=mid;
}
printf("%d\n",r);
}