Bribing FIPA (树形dp)

来源:互联网 发布:如何卸载mysql数据库 编辑:程序博客网 时间:2024/05/16 05:55

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 leastm 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 integersn (1 ≤ n ≤ 200) and m (0 ≤ mn) which are the number of countries participating in the voting process, and the number of votes Diamondland needs. The nextn lines, each describing one country, are of the following form:

CountryName DiamondCount DCName1DCName1 ...

CountryName, the name of the country, is a string of at least one and at most 100 letters andDiamondCount 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 listDCName1 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 2Aland 10Boland 20 AlandColand 15#
Sample Output
20



题目大概:

一棵树,根结点为0,只有0的子节点的获取才需要代价,而他们的子节点代价为0;问获得k个结点的最小代价是多少。

思路:

其实就是一个结点数是价值,每个结点都有价格的背包问题,不过除了第一个结点其他结点价格为0,0为根结点,只不过输入需要控制好。


代码:


#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <vector>#include <map>using namespace std;const int ma=205;int n,m,vv;int dp[ma][ma];int nao[ma];int head[ma],fat[ma];int ans;int num[ma];map<string,int>mmap;struct shu{    int v;    int next;}tr[ma<<1];void add(int q,int w){    tr[ans].v=w;    tr[ans].next=head[q];    head[q]=ans++;}void dfs(int x,int pa){    num[x]=1;    dp[x][0]=0;    for(int i=head[x];i!=-1;i=tr[i].next)    {        int son=tr[i].v;        if(pa!=son)        {            dfs(son,x);            num[x]+=num[son];            for(int j=num[x];j>=1;j--)            {                for(int k=0;k<=j&&k<=num[son];k++)                {                   dp[x][j]=min(dp[x][j],dp[x][j-k]+dp[son][k]);                }            }        }    }    dp[x][num[x]]=nao[x];}int main(){   char a[200];   char s[200],ss[200];   int sum;   while(gets(a)&&a[0]!='#')   {       //n=a[0]-'0';m=a[2]-'0';       sscanf(a,"%d%d",&n,&m);       memset(head,-1,sizeof(head));       memset(nao,0,sizeof(nao));       memset(num,0,sizeof(num));       memset(fat,-1,sizeof(fat));       memset(dp,0x3f,sizeof(dp));       mmap.clear();       ans=0;       sum=0;       for(int i=1;i<=n;i++)       {           int w;            scanf("%s%d",s,&w);            if(!mmap[s])mmap[s]=++sum;            nao[mmap[s]]=w;            while(1)            {                char c=getchar();                if(c=='\n')break;                scanf("%s",ss);                if(!mmap[ss])mmap[ss]=++sum;                add(mmap[s],mmap[ss]);                fat[mmap[ss]]=mmap[s];            }       }       num[0]=n;       for(int i=1;i<=n;i++)       {           if(fat[i]==-1)           {               add(0,i);           }       }       dfs(0,-1);       int no=0x3f3f3f;       for(int i=m;i<=n;i++)       {           no=min(no,dp[0][i]);       }       printf("%d\n",no);   }    return 0;}






原创粉丝点击