#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<utility>
#include<iomanip>
#include<queue>
using namespace std;
#define INF (1<<29)
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define FILL(a,v) memset(a,v,sizeof(a))
#define PB push_back
#define FOR(i,n) for(int i = 0;i<n;i++)
#define PI acos(-1.0)
#define EPS 1e-9
#define MP(a,b) make_pair(a,b)
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define LL long long
int cost[110],n,s,m;
vector<int>sub[110];
int memo[110][1<<9][1<<9];
int func(int pos, int bitmask1, int bitmask2)
{
if( ( bitmask1==((1<<s)-1)) && (bitmask2==((1<<s)-1)))
return 0;
if(pos>n) return 10000000;
if(memo[pos][bitmask1][bitmask2]!=-1) return memo[pos][bitmask1][bitmask2];
int tmp1, tmp2;
tmp1=bitmask1;
tmp2=bitmask2;
for(int i=0;i<sub[pos].size();i++)
{
int x=sub[pos][i];
if((tmp1 & (1<<x))==0)
tmp1 = tmp1 | (1<<x);
else
tmp2 = tmp2 | (1<<x);
}
return memo[pos][bitmask1][bitmask2]=min(func(pos+1, bitmask1, bitmask2), cost[pos]+func(pos+1, tmp1, tmp2));
}
int main()
{
string s_line;
int x,i,mask1,mask2,ans,cst;
while(cin>>s>>m>>n)
{
if(s==0) break;
s++;
ans=0;
mask1=1;
mask2=1;
for(i=1;i<=m;i++)
{
cin>>cst;
ans+=cst;
getline(cin, s_line);
istringstream ss1(s_line);
while(ss1>>x)
{
if((mask1 & (1<<x))==0)
mask1 = mask1 | (1<<x);
else
mask2 = mask2 | (1<<x);
}
}
for(i=1;i<=n;i++)
{
cin>>cost[i];
getline(cin, s_line);
istringstream ss(s_line);
while(ss>>x)
sub[i].PB(x);
}
SET(memo);
ans+=func(1, mask1, mask2);
cout<<ans<<endl;
for(i=1;i<=n;i++)
sub[i].clear();
}
}
No comments:
Post a Comment