poj3345 Bribing FIPA【树形DP】【背包】
| 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】【背包】的更多相关文章
- POJ 3345 Bribing FIPA 树形DP
题目链接: POJ 3345 Bribing FIPA 题意: 一个国家要参加一个国际组织, 需要n个国家投票, n个国家中有控制和被控制的关系, 形成了一颗树. 比如: 国家C被国家B控制, 国 ...
- URAL_1018 Binary Apple Tree 树形DP+背包
这个题目给定一棵树,以及树的每个树枝的苹果数量,要求在保留K个树枝的情况下最多能保留多少个苹果 一看就觉得是个树形DP,然后想出 dp[i][j]来表示第i个节点保留j个树枝的最大苹果数,但是在树形过 ...
- poj 3345 Bribing FIPA (树形背包dp | 输入坑)
题目链接: poj-3345 hdu-2415 题意 有n个国家,你要获取m个国家的支持,获取第i个国家的支持就要给cost[i]的价钱 其中有一些国家是老大和小弟的关系,也就是说,如果你获 ...
- POJ3345 Bribing FIPA 【背包类树形dp】
题目链接 POJ 题解 背包树形dp板题 就是读入有点无聊,浪费了很多青春 #include<iostream> #include<cstdio> #include<cm ...
- POJ3345 Bribing FIPA
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5021 Accepted: 1574 Description There ...
- hdu1561 The more, The Better (树形dp+背包)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1561 思路:树形dp+01背包 //看注释可以懂 用vector建树更简单. 代码: #i ...
- codeforces 212E IT Restaurants(树形dp+背包思想)
题目链接:http://codeforces.com/problemset/problem/212/E 题目大意:给你一个无向树,现在用两种颜色去给这颗树上的节点染色.用(a,b)表示两种颜色分别染的 ...
- BZOJ.1017.[JSOI2008]魔兽地图(树形DP 背包DP)
题目链接 树形DP,考虑子节点对父节点的贡献. 设f[x][i][j]表示当前为x,用i个x去合成上一层装备,花费为j的最大价值. 由子节点转移时 是一个分组背包,需要一个辅助数组g[i][j]表示前 ...
- joyOI 选课 【树形dp + 背包dp】
题目链接 选课 题解 基础背包树形dp #include<iostream> #include<cstdio> #include<cmath> #include&l ...
随机推荐
- (转)分析kernel的initcall函数
分析kernel的initcall函数 来源: ChinaUnix博客 日期: 2008.07.19 21:24 (共有条评论) 我要评论 分析kernel的initcall函数Autho ...
- nodejs基础 -- NPM 使用介绍
npm:是nodejs的包管理工具,随NodeJS一起安装的,能解决NodeJS代码部署上的很多问题,如: 1.允许用户从NPM服务器下载别人编写的第三方包到本地使用. 2.允许用户从NPM服务器下载 ...
- Apache -- phpmyadmin导入文件过大
方法一: 找到php.ini(或php.ini-pre1.7.2)搜索这3个地方: upload_max_filesize memory_limit post_max_size 将他们后面的值修改成大 ...
- perl 面向对象编程
今天看到一个perl面向对象编程的例子,充分体现了如何对数据进行封装: 自己模仿写一个读取配置文件的例子, 配置文件的内容如下 samtools_binary = /usr/bin/samtools ...
- Unity+NGUI性能优化方法总结
1 资源分离打包与加载 游戏中会有很多地方使用同一份资源.比如,有些界面会共用同一份字体.同一张图集,有些场景会共用同一张贴图,有些会怪物使用同一个Animator,等等.可以在制作游戏安装包时将这些 ...
- python--数据类型--1
原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 视频教程::http://www.imooc.com/learn/177 http://www. ...
- HTML&CSS精选笔记_浮动与定位
浮动与定位 元素的浮动 元素的浮动属性float 什么是浮动? 元素的浮动是指设置了浮动属性的元素会脱离标准文档流的控制,移动到其父元素中指定位置的过程. 如何定义浮动? 在CSS中,通过float属 ...
- C++11新特性之三——auto
C++11中引入的auto主要有两种用途:自动类型推断和返回值占位.auto在C++98中的标识临时变量的语义,由于使用极少且多余,在C++11中已被删除.前后两个标准的auto,完全是两个概念 1. ...
- FTP新建文件夹访问
今天在远程服务器上添加了文件夹,本来还想着FTP打开看看,结果竟然发现没有这个文件夹访问 想了一下,感觉应该是FTP访问的文件设置,只有FTP设置了的文件夹才能有显示
- PHP之变量
前面的话 变量是用于临时存储值的容器.这些值可以是数字.文本,或者复杂得多的排列组合.变量在任何编程语言中都居于核心地位,理解它们是使用php的关键所在.下面将详细介绍php中的变量 [注意]关于ja ...