#include <iostream> #include <map> #include <algorithm> #include <cstring> using namespace std; const int N =203 ,M =N; typedef long long ll; int n,m,fa[N],c[N],sz[N],f[N][N]; std::map<string,ll>p; string a[N],s[N],cur; int nxt[M],go[M],hd[N],all; void add_(int x,int y){ go[++all]=y,nxt[all]=hd[x]; hd[x]=all; } void dfs(int x){ sz[x]=1; f[x][0]=0; for(int i=hd[x];i;i=nxt[i]){ int y=go[i]; dfs(y); sz[x]+=sz[y]; } for(int i=hd[x];i;i=nxt[i]){ int y=go[i]; for(int j=sz[x];j>=0;j--) for(int k=0;k<=j;k++) f[x][j]=min(f[x][j],f[y][k]+f[x][j-k]); } if(x) for(int i=0;i<=sz[x];i++) f[x][i]=min(f[x][i],c[x]); } int main() { int i; while(std::cin>>n>>m) { memset(hd,0,sizeof hd);memset(fa,0,sizeof fa); all=0; memset(f,0x3f,sizeof f); p.clear(); for(ll i=1;i<=n;++i) { std::cin>>a[i]>>c[i]; p[a[i]]=i; s[i]=""; char ch=getchar(); if(ch==' ')ch=getchar(); while(ch!='\n'&&ch!='\r'){ s[i].push_back(ch);ch=getchar(); } } for(ll i=1;i<=n;++i) { cur=""; for(ll j=0;j<s[i].size();++j) if(s[i][j]==' ') { ll v=p[cur]; fa[v]=i; add_(i,v); cur=""; } else cur.push_back(s[i][j]); if(cur=="")continue; ll v=p[cur]; fa[v]=i; add_(i,v); } c[0]= 1e9; for(ll i=1;i<=n;++i) if(!fa[i]) add_(0,i); dfs(0); cout<<f[0][m]<<endl; } return 0; }
标签:sz,ch,FIPA,int,POJ,go,include,Bribing,hd From: https://www.cnblogs.com/towboa/p/17138517.html