Bribing FIPA
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(分组背包) POJ 3345 Bribing FIPA
最新推荐文章于 2020-08-08 21:20:12 发布
题意:给出从属关系,这里的从属关系式森林,如果选择了i节点,则i节点的所有子孙都被选择了,花费的费用为w[i] 问最少花多少费用能使得被选择的点不小于m
思路:我们用dp[i][j] 表示在i节点的子树中选择j个节点的最少费用 那么这里也是一个分组背包的问题,一个节点看成一个物品组,即把dp[son][j]看成是一个物品组,一个物品组里面最多只能选一个物品,里面的物品有 (0,dp[son][0]) (1,dp[son][1]) .....(sum[son],dp[son][sum[son]]) 前面的是cost,后面的是value, 其中sum[son]表示的是son这个子树中的节点数。
于是我们有状态转移方程 dp[father][j] = min(dp[father][j] , dp[son][k]+dp[father][j-k]);
然后这题比较麻烦的就是字符串的处理吧,也学习到了一些处理办法,不错~~~
代码:
#include<cstdio>
#include<algorithm>
#include<sstream>
#include<set>
#include<cstring>
#include<iostream>
#include<map>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
using namespace std;
const int maxn = 200+10;
const int inf = 0xfffffff;
#define LL long long
map<string,int> name;
int dp[maxn][maxn];
int sum[maxn];
bool vis[maxn];
int w[maxn];
int ind[maxn];
int ptr,n,m;
struct Node
{
int v;
Node *next;
}*first[maxn],edge[2*maxn];
void add(int x,int y)
{
edge[++ptr].next = first[x];
edge[ptr].v = y;
first[x] = &edge[ptr];
}
void dfs(int x)
{
vis[x] = true;
++sum[x];
dp[x][0] = 0;
Node *p = first[x];
while (p)
{
int v = p->v;
if (!vis[v])
{
dfs(v);
sum[x] += sum[v];
}
p = p->next;
}
p = first[x];
dp[x][sum[x]] = w[x];
while (p)
{
int v = p->v;
for (int j = n ; j >= 1 ; --j)
for (int k = 0 ; k <= sum[v] && j-k>=0 ; ++k)
dp[x][j] = min(dp[x][j],dp[x][j-k]+dp[v][k]);
p = p->next;
}
}
int main()
{
char buffer[10000];
char input[110];
string s;
//
freopen("in.txt","r",stdin);
while ( gets(buffer) )
{
if (buffer[0]=='#') return 0;
sscanf(buffer,"%d%d",&n,&m);
name.clear();
memset(w,0,sizeof(w));
memset(first,0,sizeof(first));
memset(vis,false,sizeof(vis));
memset(sum,0,sizeof(sum));
memset(ind,0,sizeof(ind));
ptr = 0;
int cnt = 0;
string s;
for (int i = 1 ; i <= n ; ++i)
{
gets(buffer);
char *tok = strtok(buffer," ");
sscanf(tok,"%s",input);
s = input;
int x , y;
if (name.count(s))
x = name[s];
else
x = name[s] = ++cnt;
tok = strtok(NULL," ");
sscanf(tok,"%d",w+x);
while (tok = strtok(NULL," "))
{
sscanf(tok,"%s",input);
s = input;
if (name.count(s))
y = name[s];
else
y = name[s] = ++cnt;
++ind[y];
add(x,y);
}
}
++cnt;
for (int i = 1 ; i <= cnt ; ++i)
for (int j = 1 ; j <= n ; ++j)
dp[i][j] = inf;
for (int i = 1 ; i < cnt ; ++i) if (!ind[i])
{
add(cnt,i);
w[cnt] += w[i];
}
sum[cnt] = -1;
dfs(cnt);
int ans = inf;
for (int i = m ; i <= n ; ++i)
ans = min(ans,dp[cnt][i]);
printf("%d\n",ans);
}
}