#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int oo=1e9;
const int mm=88888;
const int mn=444;
int node,src,dest,edge;
int ver[mm],cost[mm],flow[mm],next[mm];
int head[mn],dis[mn],q[mn],p[mn];
bool vis[mn];
void prepare(int _node,int _src,int _dest)
{
node=_node,src=_src,dest=_dest;
for(int i=0;i<node;++i)head[i]=-1;
edge=0;
}
void addedge(int u,int v,int f,int c)
{
ver[edge]=v,flow[edge]=f,cost[edge]=c,next[edge]=head[u],head[u]=edge++;
ver[edge]=u,flow[edge]=0,cost[edge]=-c,next[edge]=head[v],head[v]=edge++;
}
bool spfa()
{
int i,u,v,l,r=0,tmp;
for(i=0;i<node;++i)dis[i]=oo;
dis[q[r++]=src]=0;
p[src]=p[dest]=-1;
for(l=0;l!=r;++l>=mn?l=0:l)
for(i=head[u=q[l]],vis[u]=0;i>=0;i=next[i])
if(flow[i]&&dis[v=ver[i]]>(tmp=dis[u]+cost[i]))
{
dis[v]=tmp;
p[v]=i^1;
if(vis[v])continue;
vis[q[r++]=v]=1;
if(r>=mn)r=0;
}
return p[dest]>-1;
}
int SpfaFlow()
{
int i,ret=0,delta;
while(spfa())
{
for(i=p[dest],delta=oo;i>=0;i=p[ver[i]])
if(flow[i^1]<delta)delta=flow[i^1];
for(i=p[dest];i>=0;i=p[ver[i]])
flow[i]+=delta,flow[i^1]-=delta;
ret+=delta*dis[dest];
}
return ret;
}
int main()
{
int n,m,i,j,a,b,c;
while(~scanf("%d%d",&n,&m))
{
prepare(n+m+2,0,n+m+1);
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&c);
addedge(i,m+j,oo,c);
}
for(i=1;i<=n;i++) scanf("%d",&a),addedge(m+i,dest,a,0);
for(i=1;i<=m;i++) scanf("%d",&b),addedge(src,i,b,0);
printf("%d\n",SpfaFlow());
}
return 0;
}
nefu499最小费用最大流
最新推荐文章于 2020-07-13 12:10:09 发布