POJ3345 Bribing FIPA
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 5021 | Accepted: 1574 |
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
标准树形DP。
但是读入数据神烦,要是没有stl的map,更烦。
/*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
const int mxn=;
char s[mxn];
int cnt=;
map<string,int>mp;
vector<int>e[mxn];
bool fa[mxn];
int num[mxn];
int w[mxn];//价值
int n,m;
int f[mxn][mxn];//[根结点][选用子结点数量]=最优解
void init(){
int i,j;
for(i=;i<=n;i++)e[i].clear();
mp.clear();
memset(fa,,sizeof fa);
memset(f,0x3f,sizeof f);
memset(num,,sizeof num);
cnt=;
return;
} void dp(int rt){
// for(int i=1;i<=n;i++) f[rt][i]=INF;
f[rt][]=;
int i,j,k;
num[rt]=;
for(i=;i<e[rt].size();i++){
int v=e[rt][i];//紫树
dp(v);
num[rt]+=num[v];
for(j=n;j>=;--j){//选用子结点数量
for(k=;k<=j;++k){
f[rt][j]=min(f[rt][j],f[rt][j-k]+f[v][k]);
}
}
}
f[rt][num[rt]]=min(f[rt][num[rt]],w[rt]);
return;
}
int main(){
char str[mxn];
while(fgets(str,,stdin)){
if(str[]=='#')break;
sscanf(str,"%d%d",&n,&m);
init();
int i,j;
for(i=;i<=n;i++){
scanf("%s",s);
if(!mp.count(s)){
mp[s]=++cnt;
}
scanf("%d",&w[mp[s]]);
while(getchar()!='\n'){
scanf("%s",str);
if(!mp.count(str)){mp[str]=++cnt;}
e[mp[s]].push_back(mp[str]);
fa[mp[str]]=;
}
}
for(i=;i<=n;i++){
if(!fa[i]) e[].push_back(i);
}
w[]=INF;
dp();
int ans=INF;
for(i=m;i<=n;i++){
ans=min(ans,f[][i]);
}
printf("%d\n",ans);
}
return ;
}
POJ3345 Bribing FIPA的更多相关文章
- poj3345 Bribing FIPA【树形DP】【背包】
Bribing FIPA Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5910 Accepted: 1850 Desc ...
- POJ3345 Bribing FIPA 【背包类树形dp】
题目链接 POJ 题解 背包树形dp板题 就是读入有点无聊,浪费了很多青春 #include<iostream> #include<cstdio> #include<cm ...
- POJ3345 Bribing FIPA(树形DP)
题意:有n个国家,贿赂它们都需要一定的代价,一个国家被贿赂了从属这个国家的国家也相当于被贿赂了,问贿赂至少k个国家的最少代价. 这些国家的从属关系形成一个森林,加个超级根连接,就是一棵树了,考虑用DP ...
- POJ 3345 Bribing FIPA 树形DP
题目链接: POJ 3345 Bribing FIPA 题意: 一个国家要参加一个国际组织, 需要n个国家投票, n个国家中有控制和被控制的关系, 形成了一颗树. 比如: 国家C被国家B控制, 国 ...
- HDU 2415 Bribing FIPA
Bribing FIPA Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original I ...
- Bribing FIPA
Bribing FIPA 给出多棵有n个节点的有根树,第i个节点有一个权值\(a_i\),定义一个点能控制的点为其所有的子节点和它自己,询问选出若干个点的最少的权值之和,并且能够控制大于等于m个点,\ ...
- poj 3345 Bribing FIPA (树形背包dp | 输入坑)
题目链接: poj-3345 hdu-2415 题意 有n个国家,你要获取m个国家的支持,获取第i个国家的支持就要给cost[i]的价钱 其中有一些国家是老大和小弟的关系,也就是说,如果你获 ...
- [POJ 3345] Bribing FIPA
[题目链接] http://poj.org/problem?id=3345 [算法] 树形背包 [代码] #include <algorithm> #include <bitset& ...
- 别人整理的DP大全(转)
动态规划 动态规划 容易: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ...
随机推荐
- openstack 存储节点按照报错Device /dev/sdb not found (or ignored by filtering).
root@dell-PowerEdge-T30:~# pvcreate /dev/sdb Device /dev/sdb not found (or ignored by filtering).首页 ...
- vs和github同步开发步骤
首先,这是在visual studio中使用.需要了解关于vs同步github必不可少.下载安装破解什么的先完成vs. 1. 然后安装一个vs中使用github的插件.vs自带的下载.这个是下载地址. ...
- 使用python模拟登陆百度
#!/usr/bin/python # -*- coding: utf- -*- """ Function: Used to demostrate how to use ...
- Luogu P5349 幂
大力数学题,发现自己好久没写多项式水平急速下降,求逆都要写挂233 首先看到关于多项式的等比数列求和,我们容易想到先求出每一项的系数然后最后累加起来即可,即设\(f_i=\sum_{n=0}^{\in ...
- 数据库_8_SQL基本操作——数据操作
SQL基本操作——数据操作 一.新增数据(两种方案) 方案1: 给全表字段插入数据,不需要指定字段列表,要求数据的值出现的顺序必须与表中设计的字段出现的顺序一致,凡是非数值数据,都需要使用引号(建议是 ...
- SniperOJ-leak-advanced-x86-64
借助DynELF实现无libc的漏洞利用小结 1.leak-advance与leak的区别在于一个可用函数是write,一个可用函数是puts.write比puts更容易利用,虽然write需要的参数 ...
- shell脚本,通过传入的参数来计算最大值和最小值以及平均值。
[root@localhost zuoye]# cat quansges.sh #!/bin/bash > file [ ! $# -ge ] && || echo $* > ...
- Quartz监听的端口
上海移通短信网关:556重庆移动短信网关:557消息中心后台维护服务:558网页订单数据同步服务:559基础数据同步程序:560短信数据扣除服务:565基础数据维护服务:589推送数据抓取服务:222 ...
- JS数组专题2️⃣ ➖ 数组去重
距离上次发文,已经有一段时间了,最近工作比较忙,这不眼看快双十一了,就相当于给大家一些福利吧! 一.什么是数组去重 简单说就是把数组中重复的项删除掉,你 GET 到了吗 ?下面我将简单介绍下几种基本的 ...
- 求NEXT数组和KMP匹配的两种写法
注释掉的是我不喜欢的写法. //计算串str的next数组 void getnext(char *str){ int len=strlen(str); ,k=-; next[]=-; while(j& ...