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

Tehran 2006

标准树形DP。

但是读入数据神烦,要是没有stl的map,更烦。

 1 /*by SilverN*/
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 #include<map>
 8 #include<vector>
 9 using namespace std;
10 const int INF=0x3f3f3f3f;
11 const int mxn=300;
12 char s[mxn];
13 int cnt=0;
14 map<string,int>mp;
15 vector<int>e[mxn];
16 bool fa[mxn];
17 int num[mxn];
18 int w[mxn];//价值
19 int n,m;
20 int f[mxn][mxn];//[根结点][选用子结点数量]=最优解
21 void init(){
22     int i,j;
23     for(i=0;i<=n;i++)e[i].clear();
24     mp.clear();
25     memset(fa,0,sizeof fa);
26     memset(f,0x3f,sizeof f);
27     memset(num,0,sizeof num);
28     cnt=0;
29     return;
30 }
31
32 void dp(int rt){
33 //    for(int i=1;i<=n;i++)    f[rt][i]=INF;
34     f[rt][0]=0;
35     int i,j,k;
36     num[rt]=1;
37     for(i=0;i<e[rt].size();i++){
38         int v=e[rt][i];//紫树
39         dp(v);
40         num[rt]+=num[v];
41         for(j=n;j>=0;--j){//选用子结点数量
42             for(k=0;k<=j;++k){
43                 f[rt][j]=min(f[rt][j],f[rt][j-k]+f[v][k]);
44             }
45         }
46     }
47     f[rt][num[rt]]=min(f[rt][num[rt]],w[rt]);
48     return;
49 }
50 int main(){
51     char str[mxn];
52     while(fgets(str,200,stdin)){
53         if(str[0]==‘#‘)break;
54         sscanf(str,"%d%d",&n,&m);
55         init();
56         int i,j;
57         for(i=1;i<=n;i++){
58             scanf("%s",s);
59             if(!mp.count(s)){
60                 mp[s]=++cnt;
61             }
62             scanf("%d",&w[mp[s]]);
63             while(getchar()!=‘\n‘){
64                 scanf("%s",str);
65                 if(!mp.count(str)){mp[str]=++cnt;}
66                 e[mp[s]].push_back(mp[str]);
67                 fa[mp[str]]=1;
68             }
69         }
70         for(i=1;i<=n;i++){
71             if(!fa[i]) e[0].push_back(i);
72         }
73         w[0]=INF;
74         dp(0);
75         int ans=INF;
76         for(i=m;i<=n;i++){
77             ans=min(ans,f[0][i]);
78         }
79         printf("%d\n",ans);
80     }
81     return 0;
82 }
时间: 2024-08-04 13:55:39

POJ3345 Bribing FIPA的相关文章

poj 3345 Bribing FIPA 【树形dp + 01背包】

Bribing FIPA Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4274   Accepted: 1337 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 (Interna

HDU 2415 Bribing FIPA

Bribing FIPA Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 241564-bit integer IO format: %I64d      Java class name: Main There is going to be a voting at FIPA (Fédération Internationale de Programmation As

【树形dp】hdu 2415 Bribing FIPA

hdu 2415 Bribing FIPA 题目: 给定由若干个树组成的森林, 树上的边是有向边, 树上的每个节点都有一个代价. 若要得到某个节点, 需要付出该节点对应的代价, 若该节点拥有后继, 那么后继的节点也都能获得. 求解使用最少的代价取得至少 m 个节点 这道题输入是个问题,之前用 getchar()!='\n' 调了一晚上始终过不了,最后用C++的stringstream过了,还以为是dfs的问题,输入是巨坑!!! 说一下思路 (1)建树的时候会遇到孤立结点,设想一个虚拟节点0,没有

POJ 3345——Bribing FIPA(树形DP)

题目分析: 现在有n个村子,你想要用收买m个国家为你投票,其中收买第i个国家的代价是val[i].但是有些国家存在从属关系,如果B从属于A国,则收买了A也意味着买通了B,而且这些关系是传递的.问你最小要付出的代价是多少? 这题的难点在于怎么建图,弱菜不会,只能膜拜大神的代码,然后自己捉摸着敲,dfs部分就和一般的树形DP+背包差不多,只是状态的初始化有些变化 建图需要加个超级根,连接大国(小国和大国是从属关系) dp [ i ] [ j ]表示以 i 为根取得 j 票所需要的最小费用 大牛用了m

树形背包poj3345

Bribing FIPA Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5565   Accepted: 1762 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 (Interna

树形DP 2415HDU

Bribing FIPA Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 704    Accepted Submission(s): 251 Problem Description There is going to be a voting at FIPA (Fédération Internationale de Programmat

poj 动态规划题目列表及总结

此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276,1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740(博弈),1742, 1887, 1926(马尔科夫矩阵,求平衡), 1936, 1952, 1953, 1958, 1959, 1962, 1975,

Soj题目分类

-----------------------------最优化问题------------------------------------- ----------------------常规动态规划  SOJ1162 I-Keyboard  SOJ1685 Chopsticks SOJ1679 Gangsters SOJ2096 Maximum Submatrix  SOJ2111 littleken bg SOJ2142 Cow Exhibition  SOJ2505 The County

DP题目列表/弟屁专题

声明: 1.这份列表不是我原创的,放到这里便于自己浏览和查找题目. ※最近更新:Poj斜率优化题目 1180,2018,3709 列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740, 1742, 1887, 1926, 1936, 195