#include<cstdio>
#include<string>
#include<iostream>
#include<cstring>
#include<map>
#define MIN(x,y) ((x)>(y)?(y):(x))
#define INF 0x3f3f3f3f
using namespace std;
struct node
{
int to,next;
}e[220];
int cnt,head[220],dp[220][220],val[220],f[220],n,m;
void add_edge(int from,int to)
{
e[cnt].to=to;
e[cnt].next=head[from];
head[from]=cnt++;
}
map<string,int>t;
void dfs(int u,int v)
{
f[u]=1;
dp[u][0]=0;
for(int i=head[u];i!=-1;i=e[i].next)
{
int to=e[i].to;
if(to==v)
continue;
dfs(to,u);
f[u]+=f[to];
for(int j=f[u];j>0;j--)
{
for(int k=1;k<=f[to]&&k<=j;k++)
dp[u][j]=MIN(dp[u][j],dp[u][j-k]+dp[to][k]);
}
}
for(int i=1;i<=f[u];i++)
dp[u][i]=MIN(dp[u][i],val[u]);
}
int main()
{
char str[3000];
int tot,flag[220];
while(gets(str)&&str[0]!='#')
{
tot=0;
sscanf(str,"%d%d",&n,&m);
cnt=0;
t.clear();
memset(head,-1,sizeof(head));
memset(dp,0x3f,sizeof(dp));
memset(flag,0,sizeof(flag));
for(int i=1;i<=n;i++)
{
int v,tem1,tem2;
scanf("%s%d",str,&v);
if(t[str]==0)
{
tot++;
t[str]=tot;
}
tem1=t[str];
val[tem1]=v;
while(getchar()!='\n')
{
scanf("%s",str);
if(t[str]==0)
{
tot++;
t[str]=tot;
}
tem2=t[str];
flag[tem2]=1;
add_edge(tem1,tem2);
}
}
val[0]=INF;
for(int i=1;i<=n;i++)
if(!flag[i])
add_edge(0,i);
dfs(0,-1);
printf("%d\n",dp[0][m]);
}
}
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交