### Description

Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area there are N shopkeepers (marked from 1 to N) which stocks goods from him.Dearboy has M supply places (marked from 1 to M), each provides K different kinds of goods (marked from 1 to K). Once shopkeepers order goods, Dearboy should arrange which supply place provide how much amount of goods to shopkeepers to cut down the total cost of transport.

It's known that the cost to transport one unit goods for different kinds from different supply places to different shopkeepers may be different. Given each supply places' storage of K kinds of goods, N shopkeepers' order of K kinds of goods and the cost to transport goods for different kinds from different supply places to different shopkeepers, you should tell how to arrange the goods supply to minimize the total cost of transport.

### Input

The input consists of multiple test cases. The first line of each test case contains three integers N, M, K $(0 < N, M, K < 50)$, which are described above. The next N lines give the shopkeepers' orders, with each line containing K integers (there integers are belong to [0, 3]), which represents the amount of goods each shopkeeper needs. The next M lines give the supply places' storage, with each line containing K integers (there integers are also belong to [0, 3]), which represents the amount of goods stored in that supply place.

Then come K integer matrices (each with the size N * M), the integer (this integer is belong to (0, 100)) at the i-th row, j-th column in the k-th matrix represents the cost to transport one unit of k-th goods from the j-th supply place to the i-th shopkeeper.

The input is terminated with three "0"s. This test case should not be processed.

### Output

For each test case, if Dearboy can satisfy all the needs of all the shopkeepers, print in one line an integer, which is the minimum cost; otherwise just output "-1".

1 3 3
1 1 1
0 1 1
1 2 2
1 0 1
1 2 3
1 1 1
2 1 1

1 1 1
3
2
20

0 0 0

4
-1

### Source

POJ Monthly--2005.07.31, Wang Yijie

### Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define maxn 60
#define maxe 10010
#define INF 0x3f3f3f3f
#define DEBUG
using namespace std;
int n,m,k,cnt,ans,s,t,flag;
int need[maxn][maxn],provide[maxn][maxn],cost[maxn][maxn];

struct edge
{
int to,cap,val,next;
edge(int to=0,int cap=0,int val=0,int next=0):to(to),cap(cap),val(val),next(next) {}
}e[maxe];

{
char ch;
do
ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-');
if(ch=='-') sign=-1,ch=getchar();
while(ch>='0'&&ch<='9')
{
ch=getchar();
}
}

inline void ins(int u,int v,int cap,int val)
{
}

inline void insert(int u,int v,int cap,int val)
{ ins(u,v,cap,val);ins(v,u,0,-val); }

void build_graph(int x)
{
s=0,t=m+n+1,cnt=1;
for(int i=1;i<=n;++i) insert(m+i,t, need[i][x],0);
for(int i=1;i<=m;++i)
{
for(int j=1;j<=n;++j)
insert(i,j+m,INF,cost[j][i]);
insert(s,i,provide[i][x],0);
}
}

bool SPFA()
{
memset(dis,0x3f,sizeof(dis));
queue<int> q;
q.push(s);
dis[s]=0,prev[s]=-1;
while(!q.empty())
{
int u=q.front();q.pop();
while(i)
{
if(e[i].cap && dis[u]+e[i].val<dis[e[i].to])
{
dis[e[i].to]=dis[u]+e[i].val;
prev[e[i].to]=u;
path[e[i].to]=i;
q.push(e[i].to);
}
i=e[i].next;
}
}
if(dis[t]==INF) return 0;
return 1;
}

int argument()
{
int w=INF,cost=0;
for(int i=t;prev[i]!=-1;i=prev[i])
w=min(w,e[path[i]].cap);
for(int i=t;prev[i]!=-1;i=prev[i])
{
e[path[i]].cap-=w;
e[path[i]^1].cap+=w;
cost+=w*e[path[i]].val;
}
return cost;
}

int MCMF()
{
int ans=0;
while(SPFA()) ans+=argument();
return ans;
}

void work()
{
ans=0,flag=0;
for(int i=1;i<=k;++i) need[0][i]=provide[0][i]=0;
for(int i=1;i<=n;++i)
for(int j=1;j<=k;++j)
for(int i=1;i<=m;++i)
for(int j=1;j<=k;++j)
for(int i=1;i<=k;++i)
{
if(provide[0][i]<need[0][i]) ans=-1,flag=1;
for(int l=1;l<=n;++l)
for(int j=1;j<=m;++j)
if(!flag)
{
build_graph(i);
ans+=MCMF();
}
}
printf("%d\n",ans);
}

int main()
{
while(scanf("%d%d%d",&n,&m,&k)==3 && n)
{
work();
}
return 0;
}


Responses