poj3345 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.

典型的树形dp。用每个子节点更新父节点,枚举子节点里使用的节点数。
把所有树根连向0号结点,将森林转化为树。
注意循环顺序,避免一棵子树使用两次【类似01背包】。

#include<cstdio>
#include<cstring>
#include<vector>
#include<map>
#include<iostream>
#include<string>
using namespace std;
const int oo=0x3f3f3f3f;
int dp[210][210],cost[210],fa[210],n,m,que[210],du[210],sum[210];
vector<int> son[210];
map<string,int> mp;
char s[100010],ss[100010];
string s1,s2;
int main()
{
    int i,j,k,p,q,u,v,x,y,z,cnt,hd,tl,now;
    char c;
    while (scanf("%s",s+1)&&s[1]!='#')
    {
        sscanf(s+1,"%d",&n);
        scanf("%d",&m);
        mp.clear();
        cnt=0;
        memset(du,0,sizeof(du));
        memset(fa,0,sizeof(fa));
        for (i=1;i<=n;i++)
        {
            cin>>s1>>x;
            if (!mp.count(s1)) mp[s1]=++cnt;
            u=mp[s1];
            cost[u]=x;
            while (1)
            {
                s1="";
                c=getchar();
                while (c==' ') c=getchar();
                if (c=='\n') break;
                while (c!=' '&&c!='\n')
                {
                    s1+=c;
                    c=getchar();
                }
                if (!mp.count(s1)) mp[s1]=++cnt;
                v=mp[s1];
                fa[v]=u;
                if (c=='\n') break;
            }
        }
        memset(dp,0x3f,sizeof(dp));
        hd=1;
        tl=0;
        for (i=0;i<=n;i++) son[i].clear();
        for (i=1;i<=n;i++)
          du[fa[i]]++,son[fa[i]].push_back(i);
        for (i=0;i<=n;i++)
          if (du[i]==0)
            que[++tl]=i;
        cost[0]=oo;
        while (hd<=tl)
        {
            u=que[hd++];
            dp[u][0]=0;
            for (i=0;i<son[u].size();i++)
              for (j=m;j;j--)
                for (k=0;k<=j;k++)
                  dp[u][j]=min(min(dp[u][j],dp[u][j+1]),dp[u][j-k]+dp[son[u][i]][k]);
            sum[u]=1;
            for (i=0;i<son[u].size();i++)
              sum[u]+=sum[son[u][i]];
            for (i=sum[u];i>=0;i--)
              dp[u][i]=min(dp[u][i],cost[u]);
            du[fa[u]]--;
            if (u&&!du[fa[u]]) que[++tl]=fa[u];
        }
        printf("%d\n",dp[0][m]);
    }
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值