Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 5021   Accepted: 1574

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

Source

标准树形DP。

但是读入数据神烦,要是没有stl的map,更烦。

 /*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
const int mxn=;
char s[mxn];
int cnt=;
map<string,int>mp;
vector<int>e[mxn];
bool fa[mxn];
int num[mxn];
int w[mxn];//价值
int n,m;
int f[mxn][mxn];//[根结点][选用子结点数量]=最优解
void init(){
int i,j;
for(i=;i<=n;i++)e[i].clear();
mp.clear();
memset(fa,,sizeof fa);
memset(f,0x3f,sizeof f);
memset(num,,sizeof num);
cnt=;
return;
} void dp(int rt){
// for(int i=1;i<=n;i++) f[rt][i]=INF;
f[rt][]=;
int i,j,k;
num[rt]=;
for(i=;i<e[rt].size();i++){
int v=e[rt][i];//紫树
dp(v);
num[rt]+=num[v];
for(j=n;j>=;--j){//选用子结点数量
for(k=;k<=j;++k){
f[rt][j]=min(f[rt][j],f[rt][j-k]+f[v][k]);
}
}
}
f[rt][num[rt]]=min(f[rt][num[rt]],w[rt]);
return;
}
int main(){
char str[mxn];
while(fgets(str,,stdin)){
if(str[]=='#')break;
sscanf(str,"%d%d",&n,&m);
init();
int i,j;
for(i=;i<=n;i++){
scanf("%s",s);
if(!mp.count(s)){
mp[s]=++cnt;
}
scanf("%d",&w[mp[s]]);
while(getchar()!='\n'){
scanf("%s",str);
if(!mp.count(str)){mp[str]=++cnt;}
e[mp[s]].push_back(mp[str]);
fa[mp[str]]=;
}
}
for(i=;i<=n;i++){
if(!fa[i]) e[].push_back(i);
}
w[]=INF;
dp();
int ans=INF;
for(i=m;i<=n;i++){
ans=min(ans,f[][i]);
}
printf("%d\n",ans);
}
return ;
}

POJ3345 Bribing FIPA的更多相关文章

  1. poj3345 Bribing FIPA【树形DP】【背包】

    Bribing FIPA Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5910   Accepted: 1850 Desc ...

  2. POJ3345 Bribing FIPA 【背包类树形dp】

    题目链接 POJ 题解 背包树形dp板题 就是读入有点无聊,浪费了很多青春 #include<iostream> #include<cstdio> #include<cm ...

  3. POJ3345 Bribing FIPA(树形DP)

    题意:有n个国家,贿赂它们都需要一定的代价,一个国家被贿赂了从属这个国家的国家也相当于被贿赂了,问贿赂至少k个国家的最少代价. 这些国家的从属关系形成一个森林,加个超级根连接,就是一棵树了,考虑用DP ...

  4. POJ 3345 Bribing FIPA 树形DP

    题目链接: POJ 3345 Bribing FIPA 题意: 一个国家要参加一个国际组织,  需要n个国家投票,  n个国家中有控制和被控制的关系, 形成了一颗树. 比如: 国家C被国家B控制, 国 ...

  5. HDU 2415 Bribing FIPA

    Bribing FIPA Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original I ...

  6. Bribing FIPA

    Bribing FIPA 给出多棵有n个节点的有根树,第i个节点有一个权值\(a_i\),定义一个点能控制的点为其所有的子节点和它自己,询问选出若干个点的最少的权值之和,并且能够控制大于等于m个点,\ ...

  7. poj 3345 Bribing FIPA (树形背包dp | 输入坑)

    题目链接:  poj-3345  hdu-2415 题意 有n个国家,你要获取m个国家的支持,获取第i个国家的支持就要给cost[i]的价钱    其中有一些国家是老大和小弟的关系,也就是说,如果你获 ...

  8. [POJ 3345] Bribing FIPA

    [题目链接] http://poj.org/problem?id=3345 [算法] 树形背包 [代码] #include <algorithm> #include <bitset& ...

  9. 别人整理的DP大全(转)

    动态规划 动态规划 容易: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ...

随机推荐

  1. CloudFoundry命令行和Kubernetes命令行的Restful API消费方式

    先说CloudFoundry的命令行工具CLI.我们在CloudFoundry环境下工作,第一个使用的命令就是cf login. 如果在环境变量里维护CF_TRACE的值为true: 则我们能发现,诸 ...

  2. a标签点击后更改颜色

    function choose(id){ document.getElementById("typeid").value = id; //var infoa=document.ge ...

  3. Bootstrap历练实例:表单控件状态(禁用)

    禁用的输入框 input 如果您想要禁用一个输入框 input,只需要简单地添加 disabled 属性,这不仅会禁用输入框,还会改变输入框的样式以及当鼠标的指针悬停在元素上时鼠标指针的样式. < ...

  4. javaEE(10)_jdbc基本使用

    一.JDBC简介 1.SUN公司为了简化.统一对数据库的操作,定义了一套Java操作数据库的规范,称之为JDBC,JDBC(Java Data Base Connectivity,java数据库连接) ...

  5. vue+element UI如何导出excel表

    导出excel表应按如下规则 首先要先安装如下依赖 npm install --save xlsx npm install --save file-saver 接下在在你的代码中去引用这两个 impo ...

  6. 【树链剖分 差分】bzoj3626: [LNOI2014]LCA

    把LCA深度转化的那一步还是挺妙的.之后就是差分加大力数据结构了. Description 给出一个n个节点的有根树(编号为0到n-1,根节点为0).一个点的深度定义为这个节点到根的距离+1.设dep ...

  7. [LUOGU] P2759 奇怪的函数

    题目描述 使得 x^x x x 达到或超过 n 位数字的最小正整数 x 是多少? 输入输出格式 输入格式: 一个正整数 n 输出格式: 使得 x^xx x 达到 n 位数字的最小正整数 x 输入输出样 ...

  8. 【Java_基础】java中的常量池

    1.java常量池的介绍 java中的常量池,通常指的是运行时常量池,它是方法区的一部分,一个jvm实例只有一个运行常量池,各线程间共享该运行常量池. java常量池简介:java常量池中保存了一份在 ...

  9. Elasticsearchs的安装/laravel-scout和laravel-scout-elastic的安装

    安装: https://github.com/medcl/elasticsearch-rtf 先下载包 下载解压后 cd elasticsearch-rtf-master ll bin/elastic ...

  10. (转) [C++]我再也不想在任何头文件中看到using namespace xxx这种句子了(译)

    原文的传送:I don’t want to see another “using namespace xxx;” in a header file ever again 转自  http://blog ...