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大全(转)
动态规划 动态规划 容易: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ...
随机推荐
- App Transport Security has blocked a cleartext HTTP
打开info.plist源代码复制粘贴 <key>NSAppTransportSecurity</key> <dict> <!--Includ ...
- ucosii(2.89)semaphore 应用要点
semaphore 的作用:1,允许一个任务与其他任务(中断)同步.2,取得共享资源使用权.3,标志事件的发生.
- Bootstrap-datepicker设置开始时间结束时间范围
$('.form_datetime').datepicker({ format: 'yyyy-mm-dd', weekStart: 1, startDate: '+1', endD ...
- word2vec 中的数学原理详解(二)预备知识
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/peghoty/article/details/37969635 https://blog.csdn. ...
- Bootstrap历练实例:警告样式按钮
<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...
- 前端vue 里的tab切换 减少dom操作
<div class="vuedemo"> <div class="all"> <div class="tabone&q ...
- http常用状态吗以及分别代表什么意思?
http常用状态码: 状态码 定义 说明 1xx 信息 街道请求继续处理 2xx 成功 成功的收到.理解.接受 3xx 重定向 浏览器需要执行某些特殊处理一完成请求 4xx 客户端错误 请求的语法有问 ...
- [POJ] 1135 Domino Effect
Domino Effect Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12147 Accepted: 3046 Descri ...
- 如何用纯 CSS 创作一个蝴蝶标本展示框
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/xzgZzQ 可交互视频教 ...
- 杭电 1051 Wooden Sticks
Description There is a pile of n wooden sticks. The length and weight of each stick are known in adv ...