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. e685. 显示页面格式窗口

    The page format dialog allows the user to change the default page format values such as the orientat ...

  2. (转) 打开声音设备需要使用waveOutOpen函数

    转自:http://blog.csdn.net/nokianasty/article/details/8558151 打开声音设备 打开声音设备需要使用waveOutOpen函数(可以在您的文档中查到 ...

  3. linux -- Ubuntu查看修改mysql的登录名和密码、安装phpmyadmin

    安装好mysql后,在终端输入 mysql -u root -p 按回车,输入密码后提示access denied......ues password YES/NO的错误 原因是用户名或密码不对! 查 ...

  4. linux -- 修改文件

    vi编辑器有三种模式:命令模式,编辑模式,末行模式 打开vi后首先是命令模式,用i,o,a等进入编辑模式, 按esc退出编辑模式,回到命令模式. 在命令模式下输入:wq表示保存退出,:wq!强制保存退 ...

  5. c# 异步编程demo (async await)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  6. asp.net mvc中加入log4net记录错误日志

    直接上代码示例:https://share.weiyun.com/aff36f2547514cfefe129ebb8ccb28ef 首先添加加log4net的dll,推荐用nuget.... 贴上配置 ...

  7. 【Java面试题】47 heap和stack有什么区别

    java的内存分为两类,一类是栈内存,一类是堆内存.栈内存是指程序进入一个方法时,会为这个方法单独分配一块私属存储空间,用于存储这个方法内部的局部变量,当这个方法结束时,分配给这个方法的栈会释放,这个 ...

  8. 移动端web开发技巧 -- 转载

    META相关 1. 添加到主屏后的标题(IOS)<meta name="apple-mobile-web-app-title" content="标题"& ...

  9. mybatis由浅入深day01_9动态sql(9.5sql片段_9.6foreach)

    9 动态sql 9.1 什么是动态sql mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 9.2 需求 用户信息综合查询列表和用户信息查询列表总数这两个 ...

  10. .Net WebAPI文档自动化

    1.在VS2015中右键选择工程: 2.点击NuGet程序包: 3.输入Swagger在线搜索: 4.安装:Swagger.Net.UI和Swashbuckle包: 5.打开SwaggerNet.cs ...