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. 网络大牛如何回答Chrome的15个刁钻面试题?

    (内容来自网络整理) Google的面试题在刁钻古怪方面相当出名,甚至已经有些被神化的味道.这里整理出15道Google面试题并一一给出了网络大牛的答案,其中不少都是流传很广的. 第1题:让你清洗西雅 ...

  2. Fiddler模拟POST请求

    在进行接口测试时,会模拟post请求,发送不同的请求参数,返回不同的结果,今天我们就来分享一下,怎么用Fiddler工具模拟post请求: 打开Fiddler工具,在右侧点击“composer”的选项 ...

  3. Codeforces Round #273 (Div. 2)-C. Table Decorations

    http://codeforces.com/contest/478/problem/C C. Table Decorations time limit per test 1 second memory ...

  4. Navicat 复制多条数据

  5. shell脚本,alias别名命令用法。

    [root@localhost ~]# alias alias cp='cp -i' alias mv='mv -i' alias rm='rm -i' [root@localhost ~]# [ro ...

  6. MAC实现睡眠和休眠唤醒

    因为苹果默认为休眠文件加密,Clover 是无法解密的.所以需要经过一些设置才能破除这无节操的加密文件sleepimage.在这之前不得不提下EmuVariableUefi-64.efi 这个驱动.我 ...

  7. JavaScript reduce() 方法

    转载:http://www.runoob.com/jsref/jsref-reduce.html  JavaScript Array 对象 实例 计算数组元素相加后的总和: var numbers = ...

  8. 洛谷 P1483 序列变换

    https://www.luogu.org/problemnew/show/P1483 数据范围不是太大. 一个数组记录给k,记录每个数加了多少. 对于查询每个数的大小,那么就枚举每个数的因子,加上这 ...

  9. C++系统学习之八:IO库

    新的C++标准中有三分之二的内容都是描述标准库.接下来重点学习其中几种核心库设施,这些是应该熟练掌握的. 标准库的核心是很多容器类(顺序容器和关联容器等)和一簇泛型算法(该类算法通常在顺序容器一定范围 ...

  10. POJ-3669-流星雨

    这题的话,卡了有两个小时左右,首先更新地图的时候越界了,我们进行更新的时候,要判断一下是不是小于零了,越界就会Runtime Error. 然后bfs 的时候,我没有允许它搜出300以外的范围,然后就 ...