poj 3345 Bribing FIPA(树dp)

14 篇文章 0 订阅
Bribing FIPA
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 3244 Accepted: 1024

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 ≤ mn) 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
 
题意:有n个国家,要求至少买通m个国家,每个国家都对应一个花费,求最小代价。其中如果买通了一个国家,那么从属于它的国家也就视为被买通了,这些国家间的关系是一棵树。
思路:设dp[i][j]为买通以国家i为根的国家团体,其中在团体里的j个国家花费了钱时最小代价。
 
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <map>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define ll long long
using namespace std;

const int maxn=205;
const int INF=1000000000;
struct node
{
    int v,next;
}edge[maxn*maxn];
int head[maxn],dp[maxn][maxn],cnt[maxn],in[maxn],cost[maxn];
bool vis[maxn];
int n,m,num;
void init()
{
    memset(head,-1,sizeof(head));
    memset(in,0,sizeof(in));
    num=0;
}
void add(int u,int v)
{
    edge[num].v=v;
    edge[num].next=head[u];
    head[u]=num++;
}
void input()
{
    map<string,int>ma;
    string s1,s2;
    int id=0,c;
    char ch;
    for(int i=0;i<n;i++)
    {
        cin>>s1>>c;
        if(ma.find(s1)==ma.end()) ma[s1]=++id;
        int u=ma[s1];
        cost[u]=c;
        while((ch=getchar())!='\n')
        {
            cin>>s2;
            if(ma.find(s2)==ma.end()) ma[s2]=++id;
            int v=ma[s2];
            add(u,v);
            in[v]++;
        }
    }
}
void dfs(int u)
{
    dp[u][0]=0;
    cnt[u]=1;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        dfs(v);
        cnt[u]+=cnt[v];
        for(int k=cnt[u];k>=0;k--)
        for(int j=1;j<=k;j++)
        dp[u][k]=min(dp[u][k],dp[u][k-j]+dp[v][j]);
    }
    dp[u][cnt[u]]=cost[u];
}
void solve()
{
    memset(cnt,0,sizeof(cnt));
    for(int i=1;i<=n;i++)
    if(!in[i]) add(0,i);
    cost[0]=INF;
    for(int i=0;i<=n;i++)
    for(int j=0;j<=n;j++)
    dp[i][j]=INF;
    dfs(0);
    int ans=INF;
    for(int i=m;i<=n;i++)
    if(dp[0][i]<ans) ans=dp[0][i];
    cout<<ans<<endl;
}
int main()
{
    char s[3];
    while(cin>>s)
    {
        if(s[0]=='#') break;
        n=0;
        for(int i=0;s[i];i++)
        n=n*10+(s[i]-'0');
        cin>>m;
        init();
        input();
        solve();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值