树形背包poj3345

Posted ART_coder

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了树形背包poj3345相关的知识,希望对你有一定的参考价值。

Bribing FIPA
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 5565   Accepted: 1762

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 mcountries 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

ps:输入恶心
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<map>
using namespace std;
map<string,int>h;
int val[220];
int head[220],ver[220],nxt[220],f[220][220];
int n,m,cnt,tot;
bool v[220],vis[220];
char str[2100];
void add(int x,int y ){
  ver[++tot]=y;
  nxt[tot]=head[x];
  head[x]=tot;
}
int dfs(int x){
   vis[x]=1;
   int sum=1;
   if(!ver[head[x]]){
    f[x][1]=val[x];
    f[x][0]=0;
    return 1;
}
   f[x][0]=0;
   for(int i=head[x];i;i=nxt[i]){
    int y=ver[i];
    if(vis[y])continue;
    int t=dfs(y);
    sum+=t;
    for(int j=sum;j>=1;j--)
      for(int k=1;k<=t;k++)
        if(j>=k)f[x][j]=min(f[x][j],min(val[x],f[x][j-k]+f[y][k]));//val[x],是这道题与裸的树形dp的小小区别
   }
   return sum;
}
void ins(){//注意一下
  for(int i=0;i<=n;i++)v[i]=vis[i]=0;
  memset(head,-1,sizeof(head));
  val[0]=0x3f3f3f3f;
  cnt=0,tot=0;
  for(int i=0;i<=n;i++)
    for(int j=0;j<=m;j++)
      f[i][j]=0x3f3f3f3f;
  h.clear();
}
int main(){
  cin>>n;
  while(1){
  cin>>m;
  ins();
  for(int i=1;i<=n;i++){
       int t; 
       scanf("%s%d",str,&t);
       if(!h[str])h[str]=++cnt;//get 新技能
       int x=h[str];
       val[x]=t;
       while(getchar()!=\n){
        scanf("%s",str);
        if(!h[str])h[str]=++cnt;
        int y=h[str];
        add(x,y);
        v[y]=true;
       }
  }
    for(int i=1;i<=n;i++)if(!v[i])add(0,i);
    dfs(0);
    cout<<f[0][m]<<endl;
    scanf("%s",str);
    if(str[0]==#)break;
    n=0;
    for(int i=0;str[i];i++){
      n=n*10+str[i]-0;
    }//输入的精髓
}
    return 0;
}

 

以上是关于树形背包poj3345的主要内容,如果未能解决你的问题,请参考以下文章

POJ1155TELE[树形背包]

poj 1947 树形背包

poj2486Apple Tree[树形背包!!!]

POJ1947 Rebuilding Roads[树形背包]

POJ-1155 TELE (树形DP+分组背包)

POJ-1947 Rebuilding Roads (树形DP+分组背包)