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
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 200; 18 struct arc { 19 int v,w,f,next; 20 arc(int x = 0,int y = 0,int z = 0,int nxt = 0) { 21 v = x; 22 w = y; 23 f = z; 24 next = nxt; 25 } 26 }; 27 arc e[10000]; 28 int head[maxn],d[maxn],p[maxn],tot,n,m,k,S,T; 29 bool in[maxn]; 30 int need[maxn][maxn],supp[maxn][maxn]; 31 void add(int u,int v,int w,int f){ 32 e[tot] = arc(v,w,f,head[u]); 33 head[u] = tot++; 34 e[tot] = arc(u,-w,0,head[v]); 35 head[v] = tot++; 36 } 37 bool spfa() { 38 for(int i = S; i <= T; i++) { 39 in[i] = false; 40 d[i] = INF; 41 p[i] = -1; 42 } 43 queue<int>q; 44 d[S] = 0; 45 in[S] =true; 46 q.push(S); 47 while(!q.empty()) { 48 int u = q.front(); 49 q.pop(); 50 in[u] = false; 51 for(int i = head[u]; ~i; i = e[i].next) { 52 if(e[i].f > 0 && d[e[i].v] > d[u] + e[i].w) { 53 d[e[i].v] = d[u] + e[i].w; 54 p[e[i].v] = i; 55 if(!in[e[i].v]) { 56 q.push(e[i].v); 57 in[e[i].v] = true; 58 } 59 } 60 } 61 } 62 return p[T] > -1; 63 } 64 int solve() { 65 int tmp = 0,theMin; 66 while(spfa()) { 67 theMin = INF; 68 for(int i = p[T]; ~i; i = p[e[i^1].v]) 69 theMin = min(theMin,e[i].f); 70 for(int i = p[T]; ~i; i = p[e[i^1].v]) { 71 e[i].f -= theMin; 72 e[i^1].f += theMin; 73 tmp += e[i].w*theMin; 74 } 75 } 76 return tmp; 77 } 78 int main() { 79 bool flag; 80 int tmp,ans; 81 while(scanf("%d %d %d",&n,&m,&k),n||m||k) { 82 for(int i = 1; i <= n; i++) 83 for(int j = 1; j <= k; j++) 84 scanf("%d",need[i]+j); 85 for(int i = 1; i <= m; i++) 86 for(int j = 1; j <= k; j++) 87 scanf("%d",supp[i]+j); 88 flag = true; 89 for(int i = 1; i <= k; i++){ 90 int sum = 0; 91 for(int j = 1; j <= m; j++) 92 sum += supp[j][i]; 93 for(int j = 1; j <= n; j++) 94 sum -= need[j][i]; 95 if(sum < 0){flag = false;break;} 96 } 97 S = ans = 0; 98 T = n+m+1; 99 for(int i = 1; i <= k; i++){ 100 memset(head,-1,sizeof(head)); 101 tot = 0; 102 for(int j = 1; j <= m; j++) 103 add(S,j,0,supp[j][i]); 104 for(int j = 1; j <= n; j++){ 105 add(m+j,T,0,need[j][i]); 106 for(int t = 1; t <= m; t++){ 107 scanf("%d",&tmp); 108 add(t,m+j,tmp,INF); 109 } 110 } 111 if(flag) ans += solve(); 112 } 113 printf("%d\n",flag?ans:-1); 114 } 115 return 0; 116 }