【题目分析】
树型DP
【代码】垃圾题目
#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <sstream>
#include <vector>
using namespace std;
const int inf=0x3f3f3f3f;
int n,m,d;
map<string,int> mp;
vector<int>v[251];
int c[251];
bool r[251];
int dp[251][251];
string name;
char str[1001];
inline int dfs(int u)
{
for (int i=1;i<=n;++i) dp[u][i]=inf;
dp[u][0]=0;
int child=1;
for (int i=0;i<v[u].size();++i)
{
int to=v[u][i];
child+=dfs(to);
for (int j=n;j>=0;--j)
{
for (int k=0;k<=j;++k)
dp[u][j]=min(dp[u][j],dp[u][j-k]+dp[to][k]);
}
}
dp[u][child]=min(dp[u][child],c[u]);
return child;
}
int main()
{
while (gets(str)&&str[0]!='#')
{
sscanf(str,"%d%d",&n,&m);
int id=1;
mp.clear();
memset(r,1,sizeof r);
for (int i=0;i<=n;++i) v[i].clear();
for (int i=0;i<n;++i)
{
scanf("%s %d",&str,&d);
if (mp.find(str)==mp.end()) mp[str]=id++;//cout<<str<<" "<<mp[str]<<endl;
int u=mp[str];
c[u]=d;
gets(str);
stringstream ss(str);
while (ss>>name)
{
// cout<<"have"<<endl;
if (mp.find(name)==mp.end())
{
mp[name]=id++;
// cout<<name<<" "<<mp[name]<<endl;
}
int to=mp[name];
r[to]=false;
v[u].push_back(to);
// cout<<u<<" to "<<to<<endl;
}
}
for (int i=1;i<=n;++i)
if (r[i]) v[0].push_back(i);
c[0]=inf;
dfs(0);
int ans=inf;
for (int i=m;i<=n;++i)
ans=min(ans,dp[0][i]);
cout<<ans<<endl;
}
}