Language: C++


#include <queue>
#include <cstdio>
#include <numeric>
#include <algorithm>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

const int INF=1<<29;

const int V_MAX=1+201+200+2;
const int CAPA_INF=INF; // foolish :(
const int COST_INF=INF; // foolish :(

template<class T,class U>
struct edge{
	int v,rev;
	T capa;
	U cost;
	T flow;
};

template<class T,class U>
void add_directed_edge(vector< edge<T,U> > *G,int u,int v,T capa,U cost){
	G[u].push_back((edge<T,U>){v,G[v].size()  ,capa, cost,0});
	G[v].push_back((edge<T,U>){u,G[u].size()-1,   0,-cost,0});
}

template<class T,class U>
pair<T,U> augment(int n,vector< edge<T,U> > *G,int s,int t,U *pot){
	static int pre[V_MAX];
	static U d[V_MAX];
	rep(u,n) d[u]=(u==s?0:COST_INF);

	// Dijkstra
	bool ok=false;
	priority_queue< pair<U,int> > Q; Q.push(make_pair(0,s));
	while(!Q.empty()){
		int u=Q.top().second;
		U cost=-Q.top().first; Q.pop();

		if(cost<d[u]) continue;
		if(u==t) ok=true;

		rep(i,G[u].size()){
			edge<T,U> &e=G[u][i];
			U cost2=cost+e.cost+pot[u]-pot[e.v];
			if(e.capa-e.flow>0 && cost2<d[e.v]){
				d[e.v]=cost2;
				pre[e.v]=e.rev;
				Q.push(make_pair(-cost2,e.v));
			}
		}
	}

	if(!ok) return make_pair(0,0);

	T water=CAPA_INF;
	for(int v=t;v!=s;){
		edge<T,U> &e1=G[v][pre[v]];
		edge<T,U> &e2=G[e1.v][e1.rev];
		water=min(water,e2.capa-e2.flow);
		v=e1.v;
	}

	U cost=0;
	for(int v=t;v!=s;){
		edge<T,U> &e1=G[v][pre[v]];
		edge<T,U> &e2=G[e1.v][e1.rev];
		e1.flow-=water;
		e2.flow+=water;
		cost+=water*e2.cost;
		v=e1.v;
	}

	rep(u,n) pot[u]+=d[u];

	return make_pair(water,cost);
}

template<class T,class U>
pair<T,U> primal_dual(int n,vector< edge<T,U> > *G,int s,int t){
	static U pot[V_MAX];
	rep(u,n) pot[u]=0;

	T ans1=0;
	U ans2=0;
	while(1){
		pair<T,U> tmp=augment(n,G,s,t,pot);
		if(tmp.first==0) break;
		ans1+=tmp.first;
		ans2+=tmp.second;
	}
	return make_pair(ans1,ans2);
}

int d,k,l,c[8][8];

int num[8];
int dp[8][8][9][3*3*3*3*3*3*3*3];

inline int encode(){
	int res=0;
	rep(i,k) res=3*res+num[i];
	return res;
}

int dfs(int day,int i,int a){ // i-th parts, already bought a parts today
	if(count(num,num+k,0)==k) return 0;

	if(day==d) return INF;
	if(i==k) return dfs(day+1,0,0);

	int S=encode();
	if(dp[day][i][a][S]) return dp[day][i][a][S];

	// trivial pruning
	if(l-a+l*(d-day)<accumulate(num,num+k,0)) return INF;

	int res=dfs(day,i+1,a);
	if(num[i]>0 && a<l){
		num[i]--;
		res=min(res,dfs(day,i,a+1)+c[day][i]);
		num[i]++;
	}
	return dp[day][i][a][S]=res;
}

int main(){
	while(scanf("%d%d%d",&d,&k,&l),d){
		rep(i,d) rep(j,k) scanf("%d",c[i]+j);

		int m,n,p; scanf("%d%d%d",&m,&n,&p);
		int r[200][8],t[200][8];
		rep(i,m) rep(j,k) scanf("%d",r[i]+j);
		rep(i,p) rep(j,k) scanf("%d",t[i]+j);

		memset(dp,0,sizeof dp);

		// build graph
		vector< edge<int,int> > G[V_MAX];
		int N=1+p+1+m+2;
		int s=0,t1=N-2,t2=N-1;
		// source to fukuro
		rep(i,p) add_directed_edge(G,s,1+i,1,0);
		add_directed_edge(G,s,1+p,n,0);
		// fukuro to kadai
		rep(i,p) rep(j,m) {
			bool subset=true;
			rep(ii,k) if(t[i][ii]>r[j][ii]) subset=false;
			if(!subset) continue; // too many parts in fukuro i

			rep(ii,k) num[ii]=r[j][ii]-t[i][ii];
			int cost=dfs(0,0,0);
			if(cost<INF) add_directed_edge(G,1+i,1+p+1+j,1,cost);
		}
		// without fukuro to kadai
		rep(j,m){
			rep(ii,k) num[ii]=r[j][ii];
			int cost=dfs(0,0,0);
			if(cost<INF) add_directed_edge(G,1+p,1+p+1+j,1,cost);
		}
		// kadai to sink
		rep(j,m) add_directed_edge(G,1+p+1+j,t1,1,0);
		add_directed_edge(G,t1,t2,n,0);

		pair<int,int> ans=primal_dual(N,G,s,t2);
		printf("%d\n",ans.first<n?-1:ans.second);
	}

	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:140:17: error: 'memset' was not declared in this scope
  140 |                 memset(dp,0,sizeof dp);
      |                 ^~~~~~
a.cc:6:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
    5 | #include <algorithm>
  +++ |+#include <cstring>
    6 | 
a.cc: In instantiation of 'void add_directed_edge(std::vector<edge<T, U> >*, int, int, T, U) [with T = int; U = int]':
a.cc:147:29:   required from here
  147 |                 rep(i,p) add_directed_edge(G,s,1+i,1,0);
      |                          ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
a.cc:27:47: warning: narrowing conversion of '(G + ((sizetype)(((long unsigned int)v) * 24)))->std::vector<edge<int, int> >::size()' from 'std::vector<edge<int, int> >::size_type' {aka 'long unsigned int'} to 'int' [-Wnarrowing]
   27 |         G[u].push_back((edge<T,U>){v,G[v].size()  ,capa, cost,0});
      |                                      ~~~~~~~~~^~
a.cc:28:49: warning: narrowing conversion of '((G + ((sizetype)(((long unsigned int)u) * 24)))->std::vector<edge<int, int> >::size() - 1)' from 'std::vector<edge<int, int> >::size_type' {aka 'long unsigned int'} to 'int' [-Wnarrowing]
   28 |         G[v].push_back((edge<T,U>){u,G[u].size()-1,   0,-cost,0});
      |                                      ~~~~~~~~~~~^~