poj 3345 Bribing FIPA 树形背包

Bribing FIPA
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 4560 Accepted: 1424

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 mcountries 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



题意:现在有n个村子,你想要用收买m个村子为你投票,其中收买第i个村子的代价是val[i]。但是有些村子存在从属关系,如果B从属于A国,则收买了A也意味着买通了B,而且这些关系是传递的。问你最小要付出的代价是多少?

没啥的,“价值当体积,求得是某体积下最小花费”

背包问题中的常用把戏。

这种题目一般价值范围不明确或是浮点数。

这里总人数当背包体积,求最小花费。

因为这个题目关乎,某个“体积”(就是人数)能否达到,所以这个题目的背包意义是那种:

 某个价值能否达到(若达到,表示最小或最大花费?)。

还有一种经典的背包意义就是 一定体积的花费(或背包)至少能得到多少价值。





#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<fstream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<cctype>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI (4.0*atan(1.0))
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)
#define  lson   ind<<1,le,mid
#define rson    ind<<1|1,mid+1,ri
#define MID   int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mk    make_pair
#define _f     first
#define _s     second
#define ysk(x)  (1<<(x))
using namespace std;
//const int INF=    ;
typedef long long ll;
//const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
const int INF =0x3f3f3f3f;
const int maxn=200+10    ;
const int maxV=200+10    ;

int n,m;
int dp[maxn][maxV];
string s;
string name;
map<string,int>mp;
bool vis[maxn];
int num[maxn];
int cost[maxn];
vector<int >G[maxn];
void init()
{
    memset(vis,0,sizeof vis);
    for(int i=0;i<=n;i++)  G[i].clear();
    mp.clear();
}

int getid(string x)
{
    int m=mp.size();
    if(!mp.count(x))  return mp[x]=m+1;
    return mp[x];

}


void dfs(int x,int fa)
{
    num[x]=1;
    for(int i=0;i<G[x].size();i++)
    {
        int y=G[x][i];if(y==fa)  continue;
        dfs(y,x);
        num[x]+=num[y];
    }
    memset(dp[x],0x3f,sizeof dp[x]);
    dp[x][0]=0;
    if(x) dp[x][num[x]]=cost[x];//没这句话必错,许多思考都简单地表示在了一个或少数的几个状态转移方程中了,dp真妙
    for(int i=0;i<G[x].size();i++)
    {
        int y=G[x][i];
        for(int v=num[x];v>=0;v--)
        {
            for(int v2=0;v2<=v;v2++)  if(dp[x][v-v2]!=INF && dp[y][v2]!=INF)
            {
                dp[x][v]=min(dp[x][v],dp[x][v-v2]+dp[y][v2]);
            }
        }
    }



}
int main()
{
    while(cin>>s&&s!="#")
    {
        n=0;
        for(int i=0;i<s.length();i++)
        {
            n=n*10+s[i]-'0';
        }
        scanf("%d",&m);
        getchar();

        init();
        for(int i=1;i<=n;i++)
        {
            getline(cin,s);
            stringstream ss(s);
            ss>>name;
            int x=getid(name);
            ss>>cost[x];
            while(ss>>s)
            {
                int y=getid(s);
                G[x].push_back(y);
                vis[y]=1;
            }
        }
        for(int i=1;i<=n;i++)  if(!vis[i])
        {
            G[0].push_back(i);
        }
        dfs(0,-1);
        int ans=INF;
        for(int v=m;v<=num[0];v++)
        {
            ans=min(ans,dp[0][v]);
        }
        printf("%d\n",ans);


    }

   return 0;
}



16-7-31:

这题还很坑的

首先输入要判断是否有空行

第二次做这个题,发现这种题每个结点的背包体积一定要用子树的结点数num,不能用题目给出的m

这组样例是最好的证明

8 5
A 90 B C F
B 90
C 10 D E
D 90
E 90
F 10 G H
G 90
H 90



#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<sstream>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
#define mes(a,x,s)  memset(a,x,(s)*sizeof a[0])
#define mem(a,x)  memset(a,x,sizeof a)
typedef long long ll;
typedef pair<int, int> pii;
const int INF =0x3f3f3f3f;
const int maxn= 200   ;
int n,m;
int a[maxn+10];
int nedge,fir[maxn+10],nex[2*maxn+10],to[2*maxn+10];
int dp[maxn+10][maxn+10];

map<string ,int >mp;

int getId(string  s)
{
     if(!mp.count(s))
     {
         int k=mp.size()+1;
         mp[s]=k;
         return k;
     }
     return mp[s];
}

inline void add_edge (int x,int y)
{
    to[nedge]=y;
    nex[nedge]=fir[x];
    fir[x]=nedge++;
}

bool vis[maxn+10];


void update(int &x,int val)
{
    if(x<0||val<x)  x=val;
}



int dfs(int x)
{
    dp[x][0]=0;
   int num=1;
   for(int i=fir[x];~i;i=nex[i])
   {
       int y=to[i];
       num+=dfs(y);
       for(int v=num;v>=0;v--)//教训就是这里一定要是num,不能是m,底下给的样例是最好的证明
       {
           int ret=dp[x][v];
           for(int v2=0;v2<=v;v2++)  if(~dp[x][v-v2] &&~dp[y][v2])
           {
               update(ret,dp[y][v2]+dp[x][v-v2]);
           }
           dp[x][v]=ret;
       }
   }
//    int ret=min(m,num);
    update(dp[x][num],a[x]);
    return num;
}
int main()
{
   std::ios::sync_with_stdio(false);
    string s,nx,ny;
   while(getline(cin,s))
   {
       if(s=="#") break;
        bool ok=false;
        for(int i=0;i<s.length();i++)  if(s[i]!=' ')
        {
            ok=true;
            break;
        }
        if(!ok)  continue;
         stringstream ss(s);
        ss>>n>>m;

       mp.clear();nedge=0;
       mes(fir,-1,(n+1));


       int x;
        mes(vis,0,n+1);
       for1(i,n)
       {
           getline(cin,s);
           stringstream ss(s);
           ss>>nx;
           int idx=getId(nx);
           ss>>a[idx];
           while(ss>>ny)
           {
               int idy=getId(ny);
               add_edge(idx,idy);
               vis[idy]=1;
           }
       }

       for1(i,n)  if(!vis[i])
       {
            add_edge(0,i);
       }
       a[0]=INF;
       mem(dp,-1);
       int num=dfs(0);
       int ans=-1;
       for(int i=m;i<=num;i++) if(~dp[0][i])
       {
           update(ans,dp[0][i]);
       }

       printf("%d\n",ans);
   }
   return 0;
}
/*
8 5
A 90 B C F
B 90
C 10 D E
D 90
E 90
F 10 G H
G 90
H 90

*/


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值