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. pip 安装库过慢

    对于Python开发用户来讲,PIP安装软件包是家常便饭.但国外的源下载速度实在太慢,浪费时间.而且经常出现下载后安装出错问题. 所以把PIP安装源替换成国内镜像,可以大幅提升下载速度,还可以提高安装 ...

  2. e636. Listening to All Key Events Before Delivery to Focused Component

    Registering a key event dispatcher with the keyboard focus manager allows you to see all key events ...

  3. java设计模式——多例模式

    ★ 缓存在单例中的使用    缓存在编程中使用很频繁,有着非常重要的作用,它能够帮助程序实现以空间换取时间,通 常被设计成整个应用程序所共享的一个空间,现要求实现一个用缓存存放单例对象的类. 说明:该 ...

  4. 网络协议之socks---子网和公网的穿透

    http://www.cnblogs.com/imyijie/p/4595889.html

  5. AMD和RequireJS初识----优化Web应用前端(按需动态加载JS)

    RequireJS是一个非常小巧的JavaScript模块载入框架,是AMD规范最好的实现者之一.最新版本的RequireJS压缩后只有14K,堪称非常轻量.它还同时可以和其他的框架协同工作,使用Re ...

  6. Linux基础回想(1)——Linux系统概述

    1. 什么是操作系统?它与硬件以及其它软件之间的关系是如何的? 操作系统是控制和管理计算机系统内各种硬件和软件资源.有效组织多道程序执行的系统软件(或程序集合),是用户和计算机之间的接口.详细的说: ...

  7. 【Java面试题】13 Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以implements(实现)interface(接口)?

    1.什么是匿名内部类? 内部类,存在于另一个类内部的类,而匿名内部类,顾名思义,就是没有名字的内部类. 2.为什么需要匿名内部类? 每个inner class都能够各自继承某一实现类(implemen ...

  8. u3d调用c++ dll的DllNotFoundExceion 问题

    原文地址:http://blog.csdn.net/boren31/article/details/8778504 问题年年有,今年特别多. 开发环境: Windows  XP sp3 Visual  ...

  9. 《开源框架那些事儿22》:UI框架设计实战

    UI是User Interface的缩写.通常被觉得是MVC中View的部分,作用是提供跟人机交互的可视化操作界面. MVC中Model提供内容给UI进行渲染,用户通过UI框架产生响应,一般而言会由控 ...

  10. 重载 CreateParams 方法[1]: 从一个例子开始(取消窗口最大化、最小化按钮的三种方法)

    方法1: 使用 TForm 的 BorderIcons 属性 unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, C ...