zoukankan      html  css  js  c++  java
  • HDU 2415 Bribing FIPA

    Bribing FIPA

    Time Limit: 1000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 2415
    64-bit integer IO format: %I64d      Java class name: Main
     
    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
     
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int INF = 0x3f3f3f3f;
     4 const int maxn = 500;
     5 unordered_map<string,int>ump;
     6 vector<int>g[maxn];
     7 int n,m,w[maxn],son[maxn],dp[maxn][maxn];
     8 bool in[maxn];
     9 void dfs(int u){
    10     dp[u][0] = 0;
    11     son[u] = 1;
    12     for(int i = g[u].size()-1; i >= 0; --i){
    13         dfs(g[u][i]);
    14         son[u] += son[g[u][i]];
    15         for(int j = son[u]; j >= 0; --j)
    16             for(int k = 0; k <= j && k <= son[g[u][i]]; ++k)
    17                 dp[u][j] = min(dp[u][j],dp[u][j-k] + dp[g[u][i]][k]);
    18     }
    19     dp[u][son[u]] = min(dp[u][son[u]],w[u]);
    20 }
    21 int main(){
    22     char str[200];
    23     int id = 1,tmp,u,v;
    24     while(gets(str) && str[0] != '#'){
    25         ump.clear();
    26         memset(in,false,sizeof in);
    27         for(int i = 0; i < maxn; ++i) g[i].clear();
    28         sscanf(str,"%d%d",&n,&m);
    29         for(int i = id = 1; i <= n; ++i){
    30             scanf("%s%d",str,&tmp);
    31             if(!(u = ump[str])) u = ump[str] = id++;
    32             w[u] = tmp;
    33             while(getchar() != '
    '){
    34                 scanf("%s",str);
    35                 if(!(v = ump[str])) v = ump[str] = id++;
    36                 in[v] = true;
    37                 g[u].push_back(v);
    38             }
    39         }
    40         w[0] = INF;
    41         for(int i = 1; i < id; ++i)
    42             if(!in[i]) g[0].push_back(i);
    43         memset(dp,0x3f,sizeof dp);
    44         dfs(0);
    45         int ret = INF;
    46         for(int i = m; i <= n; ++i)
    47             ret = min(ret,dp[0][i]);
    48         printf("%d
    ",ret);
    49     }
    50     return 0;
    51 }
    View Code
  • 相关阅读:
    深圳沙龙《QTP自动化测试的技术心得》资料已上传
    TIB自动化测试快讯 自动化测试空间一周精选(20111225)
    TIB自动化测试快讯 自动化测试空间一周精选(20111127)
    ATI自动化测试杂志2011年9月刊
    广州沙龙 Selenium自动化测试实施经验分享,现已接受报名!
    广州沙龙 《测试开发》即将举行!
    北京沙龙《socket性能测试脚本的开发、监控和调优》
    AutomationQA的Selenium栏目新添加不少Selenium资料
    活动 【在线专家问答】 QA专家 张志会 与您分享QA实战经验
    广州 Selenium自动化测试 沙龙筹备中...
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4797708.html
Copyright © 2011-2022 走看看