标签:
3 2 Aland 10 Boland 20 Aland Coland 15 #
20
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int INF = 0x3f3f3f3f; 4 const int maxn = 500; 5 unordered_map<string,int>ump; 6 vector<int>g[maxn]; 7 int n,m,w[maxn],son[maxn],dp[maxn][maxn]; 8 bool in[maxn]; 9 void dfs(int u){ 10 dp[u][0] = 0; 11 son[u] = 1; 12 for(int i = g[u].size()-1; i >= 0; --i){ 13 dfs(g[u][i]); 14 son[u] += son[g[u][i]]; 15 for(int j = son[u]; j >= 0; --j) 16 for(int k = 0; k <= j && k <= son[g[u][i]]; ++k) 17 dp[u][j] = min(dp[u][j],dp[u][j-k] + dp[g[u][i]][k]); 18 } 19 dp[u][son[u]] = min(dp[u][son[u]],w[u]); 20 } 21 int main(){ 22 char str[200]; 23 int id = 1,tmp,u,v; 24 while(gets(str) && str[0] != ‘#‘){ 25 ump.clear(); 26 memset(in,false,sizeof in); 27 for(int i = 0; i < maxn; ++i) g[i].clear(); 28 sscanf(str,"%d%d",&n,&m); 29 for(int i = id = 1; i <= n; ++i){ 30 scanf("%s%d",str,&tmp); 31 if(!(u = ump[str])) u = ump[str] = id++; 32 w[u] = tmp; 33 while(getchar() != ‘\n‘){ 34 scanf("%s",str); 35 if(!(v = ump[str])) v = ump[str] = id++; 36 in[v] = true; 37 g[u].push_back(v); 38 } 39 } 40 w[0] = INF; 41 for(int i = 1; i < id; ++i) 42 if(!in[i]) g[0].push_back(i); 43 memset(dp,0x3f,sizeof dp); 44 dfs(0); 45 int ret = INF; 46 for(int i = m; i <= n; ++i) 47 ret = min(ret,dp[0][i]); 48 printf("%d\n",ret); 49 } 50 return 0; 51 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4797708.html