树形背包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;
}

原文地址:https://www.cnblogs.com/ARTlover/p/9107416.html

时间: 2024-10-07 18:46:44

树形背包poj3345的相关文章

hdu1561:树形背包dp

给定n个地点,每个地点藏有cost[i]的宝物,取得某些宝物有时需要先取其他宝物,现在让我们选m个地点问最多可以选多少宝物? 还是挺裸的树形背包dp吧,不难,关键还是中间dp的部分.可以做模板了->_-> 注意点:多组数据的话如果第一组对了然后其他都错了,那么很有可能是初始化的时候漏了.这次找可很久才知道差了e[0].clear().平时的习惯都是从1开始. --------------------------------------------------------------------

POJ 1155 树形背包

题意:从一个发射站发射电视,只有叶子节点是用户,收到一部分费用,所有的边都有花费,求在不亏本的情况下,最多可以让多少用户(叶子结点)收看到电视. 分析:树形背包. 状态定义: dp(i,j) : 以 i 为根的,让 j 个用户看到电视,最大获益(可以为负数).那么sz不再是原来的定义了. 最后遍历 j,第一个不为负数的就是答案. 状态转移:树形背包,dp(i,j) = max(d(i,j) , dp(i)(k)+dp(son,j-k)-w); #include <algorithm> #inc

【BZOJ2427】[HAOI2010]软件安装 Tarjan+树形背包

[BZOJ2427][HAOI2010]软件安装 Description 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得这些软件的价值尽可能大(即Vi的和最大).但是现在有个问题:软件之间存在依赖关系,即软件i只有在安装了软件j(包括软件j的直接或间接依赖)的情况下才能正确工作(软件i依赖软件j).幸运的是,一个软件最多依赖另外一个软件.如果一个软件不能正常工作,那么它能够发挥的作用为0.我们现在知道

【bzoj4987】Tree 树形背包dp

题目描述 从前有棵树. 找出K个点A1,A2,…,Ak. 使得∑dis(AiAi+1),(1<=i<=K-1)最小. 输入 第一行两个正整数n,k,表示数的顶点数和需要选出的点个数. 接下来n-l行每行3个非负整数x,y,z,表示从存在一条从x到y权值为z的边. I<=k<=n. l<x,y<=n 1<=z<=10^5 n <= 3000 输出 一行一个整数,表示最小的距离和. 样例输入 10 7 1 2 35129 2 3 42976 3 4 244

NYOJ 674 善良的国王(树形背包DP)

善良的国王 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 传说中有一个善良的国王Good,他为了不劳民伤财,每当建造一个城镇的时候都只用一条路去连接,这样就可以省很多的人力和物力,也就说如果有n个城镇,那么只需要n-1条路就可以把所有的城镇链接起来了(也就是一颗树了).但是不幸的事情发生了:有个一强大的帝国想要占领这个国家,但是由于国王Good的兵力不足,只能守护m个城镇,所以经过商量,国王Good只能从他的所有城镇中选择m个相链接的城市,并且把所有可以链接到这m

【bzoj4753】[Jsoi2016]最佳团体 分数规划+树形背包dp

题目描述 JSOI信息学代表队一共有N名候选人,这些候选人从1到N编号.方便起见,JYY的编号是0号.每个候选人都由一位编号比他小的候选人Ri推荐.如果Ri=0则说明这个候选人是JYY自己看上的.为了保证团队的和谐,JYY需要保证,如果招募了候选人i,那么候选人Ri"也一定需要在团队中.当然了,JYY自己总是在团队里的.每一个候选人都有一个战斗值Pi",也有一个招募费用Si".JYY希望招募K个候选人(JYY自己不算),组成一个性价比最高的团队.也就是,这K个被JYY选择的候

UVa 1407 树形背包 Caves

这道题可以和POJ 2486 树形背包DP Apple Tree比较着来做. 参考题解 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 using namespace std; 7 8 const int maxn = 500 + 10; 9 10 int n, Q; 11 vec

poj2486Apple Tree[树形背包!!!]

Apple Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9989   Accepted: 3324 Description Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to an apple tree. There are N nodes in the tree. Each node has an amoun

hdu 1011 Starship Troopers 树形背包

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 题意:有n个洞,每个洞有若干虫子和脑子,洞之间相连形成一棵树.你有m个士兵,一个士兵可以打10只虫子,士兵留下后就不能走了.从一号洞开始打,求获得最多的脑子. 树形背包.如果你在某一个洞里,有m个士兵.你可以选择派遣k个士兵去攻打与洞相连的某个子树.可以先用递归算出子树的情况.这类似于背包九讲的泛化背包. 定义 考虑这样一种物品,它并没有固定的费用和价值,而是它的价值随着你分配给它的费用而变化