Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 3550 | Accepted: 1102 |
Description
There is going to be a voting at FIPA (Fédération Internationale de Programmation Association) to determine the host of the next IPWC (International Programming World Cup). Benjamin Bennett, the delegation of Diamondland to FIPA, is trying to seek other delegation's support for a vote in favor of hosting IWPC in Diamondland. Ben is trying to buy the votes by diamond gifts. He has figured out the voting price of each and every country. However, he knows that there is no need to diamond-bribe every country, since there are small poor countries that take vote orders from their respected superpowers. So, if you bribe a country, you have gained the vote of any other country under its domination (both directly and via other countries domination). For example, if C is under domination of B, and B is under domination of A, one may get the vote of all three countries just by bribing A. Note that no country is under domination of more than one country, and the domination relationship makes no cycle. You are to help him, against a big diamond, by writing a program to find out the minimum number of diamonds needed such that at least m countries vote in favor of Diamondland. Since Diamondland is a candidate, it stands out of the voting process.
Input
The input consists of multiple test cases. Each test case starts with a line containing two integers n (1 ≤ n ≤ 200) and m (0 ≤ m ≤ n) which are the number of countries participating in the voting process, and the number of votes Diamondland needs. The next n lines, each describing one country, are of the following form:
CountryName DiamondCount DCName1 DCName1 ...
CountryName, the name of the country, is a string of at least one and at most 100 letters and DiamondCount is a positive integer which is the number of diamonds needed to get the vote of that country and all of the countries that their names come in the list DCName1 DCName1 ... which means they are under direct domination of that country. Note that it is possible that some countries do not have any other country under domination. The end of the input is marked by a single line containing a single # character.
Output
For each test case, write a single line containing a number showing the minimum number of diamonds needed to gain the vote of at least m countries.
Sample Input
3 2 Aland 10 Boland 20 Aland Coland 15 #
Sample Output
20本题输入麻烦,其他和普通树形dp一样
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <queue> #include <algorithm> #include <map> #include <sstream> #include <cmath> #include <climits> #include <iomanip> #define INF 99999999 typedef long long LL; using namespace std; const int MAX=200+10; int n,m,size,val[MAX],pre[MAX]; int dp[MAX],temp[MAX],head[MAX]; char s[2222],name[110]; map<string,int>Map; struct Edge{ int v,next; Edge(){} Edge(int V,int NEXT):v(V),next(NEXT){} }edge[MAX]; void Init(int num){ for(int i=0;i<=num;++i)head[i]=-1,pre[i]=0; size=0; } void InsertEdge(int u,int v){ edge[size]=Edge(v,head[u]); head[u]=size++; } int dfs(int u,int Id){ int last=0,now=0; dp[Id]=0; for(int i=head[u];i != -1;i=edge[i].next){ int v=edge[i].v; now=dfs(v,Id+last+1)+1; for(int j=last+1;j<=last+now;++j)dp[Id+j]=INF; for(int j=last;j>=0;--j){ for(int k=1;k<now;++k){ dp[Id+j+k]=min(dp[Id+j+k],dp[Id+j]+temp[k]); } dp[Id+j+now]=min(dp[Id+j+now],dp[Id+j]+val[v]); } last+=now; } for(int i=1;i<=last;++i)temp[i]=dp[Id+i]; return last; } int main(){ int u,v; stringstream Cin; while(gets(s)){ if(s[0] == '#')break; Map.clear(); sscanf(s,"%d%d",&n,&m); Init(n); int k=0; for(int i=1;i<=n;++i){ gets(s); Cin.clear(); Cin.str(s); Cin>>name; u=Map[name]; if(u == 0)Map[name]=++k,u=k; Cin>>val[u]; while(Cin>>name){ v=Map[name]; if(v == 0)Map[name]=++k,v=k; InsertEdge(u,v); pre[v]=u; } } for(int i=1;i<=n;++i)if(!pre[i])InsertEdge(0,i); dfs(0,0); int sum=INF; for(int i=m;i<=n;++i)if(sum>temp[i])sum=temp[i]; printf("%d\n",sum); } return 0; }