Bribing FIPA

Time Limit: 1000ms
Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 2415
64-bit integer IO format: %I64d      Java class name: Main

 
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
 
 #include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = ;
unordered_map<string,int>ump;
vector<int>g[maxn];
int n,m,w[maxn],son[maxn],dp[maxn][maxn];
bool in[maxn];
void dfs(int u){
dp[u][] = ;
son[u] = ;
for(int i = g[u].size()-; i >= ; --i){
dfs(g[u][i]);
son[u] += son[g[u][i]];
for(int j = son[u]; j >= ; --j)
for(int k = ; k <= j && k <= son[g[u][i]]; ++k)
dp[u][j] = min(dp[u][j],dp[u][j-k] + dp[g[u][i]][k]);
}
dp[u][son[u]] = min(dp[u][son[u]],w[u]);
}
int main(){
char str[];
int id = ,tmp,u,v;
while(gets(str) && str[] != '#'){
ump.clear();
memset(in,false,sizeof in);
for(int i = ; i < maxn; ++i) g[i].clear();
sscanf(str,"%d%d",&n,&m);
for(int i = id = ; i <= n; ++i){
scanf("%s%d",str,&tmp);
if(!(u = ump[str])) u = ump[str] = id++;
w[u] = tmp;
while(getchar() != '\n'){
scanf("%s",str);
if(!(v = ump[str])) v = ump[str] = id++;
in[v] = true;
g[u].push_back(v);
}
}
w[] = INF;
for(int i = ; i < id; ++i)
if(!in[i]) g[].push_back(i);
memset(dp,0x3f,sizeof dp);
dfs();
int ret = INF;
for(int i = m; i <= n; ++i)
ret = min(ret,dp[][i]);
printf("%d\n",ret);
}
return ;
}

HDU 2415 Bribing FIPA的更多相关文章

  1. POJ 3345 Bribing FIPA 树形DP

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

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

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

  3. Bribing FIPA

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

  4. POJ3345 Bribing FIPA

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

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

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

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

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

  7. POJ3345 Bribing FIPA(树形DP)

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

  8. [POJ 3345] Bribing FIPA

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

  9. 【转】【DP_树形DP专辑】【9月9最新更新】【from zeroclock's blog】

    树,一种十分优美的数据结构,因为它本身就具有的递归性,所以它和子树见能相互传递很多信息,还因为它作为被限制的图在上面可进行的操作更多,所以各种用于不同地方的树都出现了,二叉树.三叉树.静态搜索树.AV ...

随机推荐

  1. 解析Qt元对象系统(五) Q_INVOKABLE与invokeMethod(automatic connection从Qt4.8开始的解释已经与之前不同,发送对象驻足于哪一个线程并不重要,起到决定作用的是接收者对象所驻足的线程以及发射信号(该信号与接受者连接)的线程是不是在同一个线程)good

    概述查看Qt源码可知,Q_INVOKABLE是个空宏,目的在于让moc识别. 使用Q_INVOKABLE来修饰成员函数,目的在于被修饰的成员函数能够被元对象系统所唤起. Q_INVOKABLE与QMe ...

  2. Angularjs+Bootstrap实现分页指令

    本插件的开发目的主要给前端同学使用,本人是专注于后台开发的,对css样式不熟悉,但逼于前端要求做一个共公组件方便日常开发,所以这个插件在样式上可能不适合大部分人,喜欢的拿走吧,不喜欢的也请别喷.  一 ...

  3. python3用list实现栈

    工作中遇到的需求,****代表标签数据别的信息: D01 ******** 1 ******** D01 ******** 2 ******** D01 ******** 3 ******** D01 ...

  4. rpmbuild

    rpm2cpio xxx.rpm | cpio -div

  5. 85.Ext.Window

    转自:https://chenjumin.iteye.com/blog/668421 1.主要配置项:       closable:是否允许关闭窗口,默认为true.       closeActi ...

  6. Hamming Distance(随机算法)

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 题意:计算任意两个十六进制的数异或后1的最少个数. 思路:用随机数随机产生两个数作为下标,记录这两个数异或 ...

  7. 关于函数提升在if语句中的表现

    函数声明创建的函数在现代浏览器,在if语句中函数的声明不会提升,但是在老的IE版本中,if语句中的函数声明会提升 函数表达式在不同浏览器中函数声明都不会被提升,解决了不同浏览器的兼容性问题 关于函数提 ...

  8. Cracking the Coding Interview 8.5

    Implement an algorithm to print all valid combinations of n-pairs of parentheses #include<stdio.h ...

  9. Windows phone开发之文件夹与文件操作系列(一)文件夹与文件操作

    Windows phone7中文件的存储模式是独立的,即独立存储空间(IsolatedStorage).对文件夹与文件操作,需要借助IsolatedStorageFile类. IsolatedStor ...

  10. MSCRM4 在过滤后的LOOKUP框中实现查找

    在MSCRM中让Lookup根据一定的条件实现过滤功能, 这个需求很常见, 在我接触的诸多项目中似乎都需要有这个功能. 但非常遗憾是, MSCRM 的SDK并没有提供实现这个功能的方法. 不过我们应该 ...