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的更多相关文章

  1. poj3345 Bribing FIPA【树形DP】【背包】

    Bribing FIPA Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5910   Accepted: 1850 Desc ...

  2. POJ3345 Bribing FIPA 【背包类树形dp】

    题目链接 POJ 题解 背包树形dp板题 就是读入有点无聊,浪费了很多青春 #include<iostream> #include<cstdio> #include<cm ...

  3. POJ3345 Bribing FIPA(树形DP)

    题意:有n个国家,贿赂它们都需要一定的代价,一个国家被贿赂了从属这个国家的国家也相当于被贿赂了,问贿赂至少k个国家的最少代价. 这些国家的从属关系形成一个森林,加个超级根连接,就是一棵树了,考虑用DP ...

  4. POJ 3345 Bribing FIPA 树形DP

    题目链接: POJ 3345 Bribing FIPA 题意: 一个国家要参加一个国际组织,  需要n个国家投票,  n个国家中有控制和被控制的关系, 形成了一颗树. 比如: 国家C被国家B控制, 国 ...

  5. HDU 2415 Bribing FIPA

    Bribing FIPA Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original I ...

  6. Bribing FIPA

    Bribing FIPA 给出多棵有n个节点的有根树,第i个节点有一个权值\(a_i\),定义一个点能控制的点为其所有的子节点和它自己,询问选出若干个点的最少的权值之和,并且能够控制大于等于m个点,\ ...

  7. poj 3345 Bribing FIPA (树形背包dp | 输入坑)

    题目链接:  poj-3345  hdu-2415 题意 有n个国家,你要获取m个国家的支持,获取第i个国家的支持就要给cost[i]的价钱    其中有一些国家是老大和小弟的关系,也就是说,如果你获 ...

  8. [POJ 3345] Bribing FIPA

    [题目链接] http://poj.org/problem?id=3345 [算法] 树形背包 [代码] #include <algorithm> #include <bitset& ...

  9. 别人整理的DP大全(转)

    动态规划 动态规划 容易: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ...

随机推荐

  1. hadoop中修改端口号

    1.hdfs-site.xml 这里修改hdfs相关的端口. 1 <property> 2 <name>dfs.namenode.scondary.http-address&l ...

  2. Codeforces Round #290 (Div. 2) _B找矩形环的三种写法

    http://codeforces.com/contest/510/status/B 题目大意 给一个n*m  找有没有相同字母连起来的矩形串 第一种并查集 瞎搞一下 第一次的时候把val开成字符串了 ...

  3. solr scheme配置简介

    solr 字段配置,和数据库数据索引配置 配置solr字段. schema.xml 文件里配置 先讲解一下,里面的一些字段 1. <types> ... </types> 表示 ...

  4. python基础一 day11 装饰器复习

    # 复习# 讲作业# 装饰器的进阶 # functools.wraps # 带参数的装饰器 # 多个装饰器装饰同一个函数# 周末的作业 # 文件操作 # 字符串处理 # 输入输出 # 流程控制 # 装 ...

  5. 激活 IDEA, PyCharm

    1. 到网站 http://idea.lanyus.com/ 获取注册码. 2.填入下面的license server: http://intellij.mandroid.cn/ http://ide ...

  6. SNP|RELP|genetic polymorphism|

    5.3个体基因组呈现广泛变化 遗传多态性:一个基因座上存在多个等位基因(因为野生型不止一种基因)的现象,但是只有这多种等位基因满足:1.多个基因稳定存在2.基因在种群中数目大于1%时,认为该基因座多态 ...

  7. java在线聊天项目 实现基本聊天功能后补充的其他功能详细需求分析 及所需要掌握的Java知识基础 SWT的激活方法,swt开发包下载,及破解激活码

    补充聊天项目功能,做如下需求分析: 梳理项目开发所需的必要Java知识基础 GUI将使用更快速的swt实现 SWT(Standard Widget Toolkit) Standard Widget T ...

  8. ios面试题(三)

    4.写一个setter方法用于完成@property (nonatomic,retain)NSString *name,写一个setter方法用于完成@property(nonatomic,copy) ...

  9. jwt 登录

    /* eslint-disable */ 'use strict'; const Controller = require('egg').Controller; const jwt = require ...

  10. Linux三剑客之sed详解(2)

    一.sed 分组替换(),\1 实例:I am a oldboy teacher. 吧oldboy 提取出来 二.特殊符号&代表被替换的字符串 实例:批量替换文件名 把stu_102999_1 ...