码迷,mamicode.com
首页 > 其他好文 > 详细

树形背包poj3345

时间:2018-05-29 19:41:12      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:ever   mis   amp   hat   mini   div   ssi   break   process   

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

ps:输入恶心
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<map>
using namespace std;
map<string,int>h;
int val[220];
int head[220],ver[220],nxt[220],f[220][220];
int n,m,cnt,tot;
bool v[220],vis[220];
char str[2100];
void add(int x,int y ){
  ver[++tot]=y;
  nxt[tot]=head[x];
  head[x]=tot;
}
int dfs(int x){
   vis[x]=1;
   int sum=1;
   if(!ver[head[x]]){
    f[x][1]=val[x];
    f[x][0]=0;
    return 1;
}
   f[x][0]=0;
   for(int i=head[x];i;i=nxt[i]){
    int y=ver[i];
    if(vis[y])continue;
    int t=dfs(y);
    sum+=t;
    for(int j=sum;j>=1;j--)
      for(int k=1;k<=t;k++)
        if(j>=k)f[x][j]=min(f[x][j],min(val[x],f[x][j-k]+f[y][k]));//val[x],是这道题与裸的树形dp的小小区别
   }
   return sum;
}
void ins(){//注意一下
  for(int i=0;i<=n;i++)v[i]=vis[i]=0;
  memset(head,-1,sizeof(head));
  val[0]=0x3f3f3f3f;
  cnt=0,tot=0;
  for(int i=0;i<=n;i++)
    for(int j=0;j<=m;j++)
      f[i][j]=0x3f3f3f3f;
  h.clear();
}
int main(){
  cin>>n;
  while(1){
  cin>>m;
  ins();
  for(int i=1;i<=n;i++){
       int t; 
       scanf("%s%d",str,&t);
       if(!h[str])h[str]=++cnt;//get 新技能
       int x=h[str];
       val[x]=t;
       while(getchar()!=\n){
        scanf("%s",str);
        if(!h[str])h[str]=++cnt;
        int y=h[str];
        add(x,y);
        v[y]=true;
       }
  }
    for(int i=1;i<=n;i++)if(!v[i])add(0,i);
    dfs(0);
    cout<<f[0][m]<<endl;
    scanf("%s",str);
    if(str[0]==#)break;
    n=0;
    for(int i=0;str[i];i++){
      n=n*10+str[i]-0;
    }//输入的精髓
}
    return 0;
}

 

树形背包poj3345

标签:ever   mis   amp   hat   mini   div   ssi   break   process   

原文地址:https://www.cnblogs.com/ARTlover/p/9107416.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!