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

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

题意:

给定一棵树,节点之间有隶属关系,每个节点有一个贿赂值。当你贿赂了一个节点之后,他的所有子孙也都被你贿赂了就不需要贿赂了。现在想找m个国家给你投票,只有贿赂了他们才会给你投票,希望花费的贿赂值是最小的。

思路:

这道题疯狂RE,读入太毒了,后来发现其实是map和vector可能没清干净。要注意!

一个树上的背包。不太会写。

总的只有n个节点,就是体积。价值就是贿赂值,但是希望是尽量小。

 //#include <bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<stdio.h>
#include<cstring>
#include<vector>
#include<map>
#include<set> #define inf 0x3f3f3f3f
using namespace std;
typedef long long LL; int n, m, cnt = ;
const int maxn = ;
map<string, int>mp;
struct node{
//int id;
int diamond;
vector<int>son;
}country[maxn];
bool vis[maxn];
int dp[maxn], temp[maxn]; int dfsdp(int rt, int ID)
{
int last = , now = ;//last和now相当于dfs序,用来表示rt子树的一个区间
dp[ID] = ;
for(int i = ; i < country[rt].son.size(); i++){
int child = country[rt].son[i];
now = dfsdp(child, ID + last + ) + ;//id表示这是当前第几个城市
for(int j = last + ; j <= last + now; j++){
dp[ID + j] = inf;
}
for(int j = last; j >= ; j--){
for(int k = ; k < now; k++){
dp[ID + j + k] = min(dp[ID + j + k], dp[ID + j] + temp[k]);
}
dp[ID + j + now] = min(dp[ID + j + now], dp[ID + j] + country[child].diamond);
}
last += now;
} for(int i = ; i <= last; i++){
temp[i] = dp[ID + i];
}
return last;
} int read()
{
char c = getchar();
int s = ;
while(c < '' || c > ''){
if(c == '#')return ;
c = getchar();
}
while(c >= '' &&c <= ''){
s *= ;
s += c - '';
c = getchar();
}
return s;
} int main(){
//char ch;
char str[];
bool flag = true;
while(gets(str)){
if(str[] == '#')break;
sscanf(str, "%d%d", &n, &m);
cnt = ;
memset(vis, false, sizeof(vis));
mp.clear();
for(int i = ; i <= n; i++){
country[i].son.clear();
}
for(int i = ; i < n; i++){
scanf("%s", str);
int d;
if(mp.find(str) == mp.end()){
mp[str] = ++cnt;
}
scanf("%d", &d);
int now = mp[str];
country[mp[str]].diamond = d;
//getchar();
while(getchar() != '\n'){
scanf("%s", str);
if(mp.find(str) == mp.end()){
mp[str] = ++cnt;
}
//cout<<mp[name]<<endl;
country[now].son.push_back(mp[str]);
vis[mp[str]] = true;
}
}
for(int i = ; i <= cnt; i++){
if(!vis[i]){
country[].son.push_back(i);
}
}
country[].diamond = inf;
dfsdp(, );
int ans = inf;
for(int i = m; i <= n; i++){
ans = min(ans, dp[i]);
}
printf("%d\n", ans); } return ;
}

poj3345 Bribing FIPA【树形DP】【背包】的更多相关文章

  1. POJ 3345 Bribing FIPA 树形DP

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

  2. URAL_1018 Binary Apple Tree 树形DP+背包

    这个题目给定一棵树,以及树的每个树枝的苹果数量,要求在保留K个树枝的情况下最多能保留多少个苹果 一看就觉得是个树形DP,然后想出 dp[i][j]来表示第i个节点保留j个树枝的最大苹果数,但是在树形过 ...

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

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

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

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

  5. POJ3345 Bribing FIPA

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

  6. hdu1561 The more, The Better (树形dp+背包)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1561 思路:树形dp+01背包 //看注释可以懂 用vector建树更简单. 代码: #i ...

  7. codeforces 212E IT Restaurants(树形dp+背包思想)

    题目链接:http://codeforces.com/problemset/problem/212/E 题目大意:给你一个无向树,现在用两种颜色去给这颗树上的节点染色.用(a,b)表示两种颜色分别染的 ...

  8. BZOJ.1017.[JSOI2008]魔兽地图(树形DP 背包DP)

    题目链接 树形DP,考虑子节点对父节点的贡献. 设f[x][i][j]表示当前为x,用i个x去合成上一层装备,花费为j的最大价值. 由子节点转移时 是一个分组背包,需要一个辅助数组g[i][j]表示前 ...

  9. joyOI 选课 【树形dp + 背包dp】

    题目链接 选课 题解 基础背包树形dp #include<iostream> #include<cstdio> #include<cmath> #include&l ...

随机推荐

  1. Netstat命令(windows下)

    功能: 一般用于检验本机各端口的网络连接情况. 例子:检查本机3389远程连接端口是否可用 netstat -nao|find  "3389" 查看某进程使用的端口号: netst ...

  2. linux -- 进程管理和作业控制

    一. 作业控制 1. 直接将命令放到后台"执行": &  [root @test /root ]# command & 范例: [root @test /root] ...

  3. 学习:erlang的不定长数据包头部。

  4. 安装 oracle [转]

    先下载3个东西:链接忘记了,大家自己找一下 1  ORA+11+G+R2+server+64bit+for+windows.iso  (Oracle 安装文件) 2  PLSql 3  oracle6 ...

  5. c++ 类型定义

    1. typedef map<int, CString> UDT_MAP_INT_CSTRING; UDT_MAP_INT_CSTRING enumMap;

  6. 阮一峰---javascript系列

    2013.05.11:如何做到 jQuery-free?(29条评论) 2013.01.23:JavaScript Source Map 详解(14条评论) 2013.01.14:Javascript ...

  7. mybatis由浅入深day01_6SqlMapConfig.xml(6.2settings全局参数配置_6.3typeAliases(类型别名)_6.4typeHandlers(类型处理器)_6.5mappers(映射配置))

    6 SqlMapConfig.xml mybatis的全局配置文件SqlMapConfig.xml,配置内容和顺序如下: properties(属性) settings(全局配置参数) typeAli ...

  8. HTML&CSS精选笔记_列表与超链接

    列表与超链接 列表标记 无序列表ul 无序列表的各个列表项之间没有顺序级别之分,是并列的 <ul> <li>列表项1</li> <li>列表项2< ...

  9. try catch finally的执行顺序

    1.将预见可能引发异常的代码包含在try语句块中. 2.如果发生了异常,则转入catch的执行.catch有几种写法: catch 这将捕获任何发生的异常. catch(Exception e) 这将 ...

  10. NUC972学习历程之NUWRITER使用说明以及烧录模式的说明

    3.1 簡介Nu-Writer 工具能幫助使用者透過 USB ISP模式, 將Image檔案放入儲存體中, 例如:SPI Flash設備或 NAND Flash設備.3.2 驅動程式安裝Nu-Writ ...