和修车几乎一样
考虑一个动态加边
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
const int N=40600;
const int INF=2139062043;
int n,m,s,t,ans,tot[N],cnt;
int map[110][110];
int dis[N],id[N],num[N];
bool pd[N];
struct hh{int x,y,l,c;}nex[N<<6];
int fir[N],SIZE=1;
void add(int x,int y,int l,int c)
{
if(l<=0) return;
nex[++SIZE]=(hh){fir[x],y,l,c};fir[x]=SIZE;
nex[++SIZE]=(hh){fir[y],x,0,-c};fir[y]=SIZE;
}
bool spfa()
{
deque<int>q;
fill(dis,dis+cnt+1,INF);
fill(pd,pd+cnt+1,0);
q.push_back(t);dis[t]=0;
while(!q.empty())
{
int x=q.front();q.pop_front();pd[x]=0;
for(int i=fir[x];i;i=nex[i].x)
{
int to=nex[i].y;
if(nex[i^1].l&&dis[to]>dis[x]-nex[i].c)
{
dis[to]=dis[x]-nex[i].c;
if(!pd[to])
{
pd[to]=1;
if(!q.empty()&&dis[to]<=dis[q.front()]) q.push_front(to);
else q.push_back(to);
}
}
}
}
return dis[s]<INF;
}
void build(int j,int k)
{
add(++cnt,t,1,0);
for(int i=1;i<=n;++i) add(i,cnt,1,k*map[i][j]);
id[cnt]=j;num[cnt]=k;
}
int dfs(int x,int f)
{
pd[x]=1;
if(x==t) return f;
int dat=0;
for(int i=fir[x];i;i=nex[i].x)
{
int to=nex[i].y;
if(nex[i].l&&dis[to]==dis[x]-nex[i].c&&!pd[to])
{
int w=dfs(to,min(f-dat,nex[i].l));
if(id[x]&&w) build(id[x],num[x]+1),id[x]=num[x]=0;
dat+=w,nex[i].l-=w,nex[i^1].l+=w;
ans+=w*nex[i].c;
if(dat==f) return f;
}
}
return dat;
}
void zkw()
{
while(spfa())
{
pd[t]=1;
while(pd[t])
{
fill(pd,pd+cnt+1,0);
dfs(s,INF);
}
}
}
int main()
{
//freopen("a.in","r",stdin);
int i,j;
scanf("%d%d",&n,&m);s=0;t=n+1;cnt=n+1;
for(i=1;i<=n;++i) scanf("%d",&tot[i]);
for(i=1;i<=n;++i)
{
add(s,i,tot[i],0);
for(j=1;j<=m;++j) scanf("%d",&map[i][j]);
}
for(j=1;j<=m;++j) build(j,1);
zkw();
cout<<ans;
return 0;
}