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 ...
随机推荐
- php -- 取日期
1.获取当前时间方法date()很简单,这就是获取时间的方法, 格式为:date($format, $timestamp), format为格式 - 必需 timestamp为时间戳–可填参数. 比如 ...
- Python C :ctypes库
>>> import ctypes >>> from ctypes import * >>> dir(ctypes) ['ARRAY', 'Arg ...
- CentOS tree命令详解
inux下tree命令详解---linux以树状图逐级列出目录的内容命令 ############################################################### ...
- asp.net MVC提高开发速度(创建项目模板)
- 【Java面试题】47 heap和stack有什么区别
java的内存分为两类,一类是栈内存,一类是堆内存.栈内存是指程序进入一个方法时,会为这个方法单独分配一块私属存储空间,用于存储这个方法内部的局部变量,当这个方法结束时,分配给这个方法的栈会释放,这个 ...
- PHP实现金额数字转换成大写函数
<?php header("Content-Type:text/html;charset=utf-8"); function num_to_upper($num) { $d ...
- 动态生成的DOM不会触发onclick事件的原因及解决方法
最近朋友在做一个项目的时候,遇到动态加载微博内容,然后点击“展开评论”后获取该微博的所有评论.这里使用了动态加载的<span mid='123456789′ class='get_comment ...
- 使用Visual Studio将C#生成DLL文件的方法
1.命令方式 打开Visual Studio安装目录下的开发人员命令提示 译 File.cs 以产生 File.exe csc File.cs 编译 File.cs 以产生 File.dll csc ...
- C/C++ 控制台演示彩色输出进度
#include <stdio.h> #include <windows.h> BOOL SetConsoleColor(WORD wAttributes); int main ...
- 九度 1537:买卖股票(区间DP)
总结 1. 更新动规矩阵时, 不要 push 更新, 要用 pull更新. push 更新容易让逻辑出问题, 自己卡了很久, 改用 pull 就变得很顺利了 2. acm 题, 空间至多是百万, 再网 ...