程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> poj3345Bribing FIPA(樹形DP,一道不錯的題)

poj3345Bribing FIPA(樹形DP,一道不錯的題)

編輯:C++入門知識

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 ≤ mn) 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

Source

Tehran 2006
#include
#include
#include
#include
#include
#include
using namespace std;
#define inf 1000000000
int n,M,dp[205][205],k[205],cost[205],vist[205];
vectortree[205];
mapindex;

void init(int n)
{
    index.clear();
    for(int i=0; i<=n;i++)
    {
        cost[i]=inf; vist[i]=0;
        tree[i].clear();
    }
}
void dfs(int p)
{
    int len=tree[p].size();
    dp[p][0]=0; k[p]=0; vist[p]=1;
    for(int i=1;i<=n;i++)
    dp[p][i]=cost[p];
    for(int i=0;i0; m--)
            for(int s=1;s<=k[son]&&s<=m;s++)
            if(dp[p][m]>dp[p][m-s]+dp[son][s])
            dp[p][m]=dp[p][m-s]+dp[son][s];
    }
    dp[p][++k[p]]=cost[p];
}

int main()
{
    char nn[5],name[105];
    int c,i;
    while(scanf("%s",nn)>0&&strcmp(nn,"#")!=0)
    {
        scanf("%d",&M);
        n=0;
        for( i=0; nn[i]!='\0'; i++)
        n=n*10+nn[i]-'0';

        init(n);
        int ind=0;
        for(i=1;i<=n;i++)
        {
            scanf("%s%d",name,&c);
            if(index[name]==0) {
                ind++; index[name]=ind;
            }
            int fath=index[name],son;
            cost[fath]=c;

            while(getchar()!='\n')
            {
                scanf("%s",name);
                if(index[name]==0){
                    ind++; index[name]=ind;
                }
                son=index[name]; vist[son]=1;
                tree[fath].push_back(son);
            }
        }
        cost[0]=0;
        for(i=1; i<=n;i++){
            if(vist[i]==0)
                cost[0]+=cost[i],tree[0].push_back(i);
            vist[i]=0;
        }

        dfs(0);
        int ans=dp[0][M];
        for( i=M+1; i<=n; i++)
        if(dp[0][i]

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved