poj 3345 Bribing FIPA

来源:互联网 发布:儒尼尼奥任意球数据 编辑:程序博客网 时间:2024/04/23 18:05

Bribing FIPA

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 5015 Accepted: 1573

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.

Sample Input
3 2
Aland 10
Boland 20 Aland
Coland 15
#

Sample Output
20


【分析】
智障题…本质是树形dp,然而读入太迷,感觉浪费时间…于是抄代码弃坑。


【代码】

#include <cstdio>#include <cmath>#include <cstring>#include <iostream>#include <algorithm>#include <string>#include <map>#include <sstream>#include <vector>using namespace std;const int inf=0x3f3f3f3f;int n,m,d;map<string,int> mp;vector<int>v[251];int c[251];bool r[251];int dp[251][251];string name;char str[1001];inline int dfs(int u){    for (int i=1;i<=n;++i) dp[u][i]=inf;    dp[u][0]=0;    int child=1;    for (int i=0;i<v[u].size();++i)    {        int to=v[u][i];        child+=dfs(to);        for (int j=n;j>=0;--j)        {            for (int k=0;k<=j;++k)                dp[u][j]=min(dp[u][j],dp[u][j-k]+dp[to][k]);        }    }    dp[u][child]=min(dp[u][child],c[u]);    return child;}int main(){    while (gets(str)&&str[0]!='#')    {        sscanf(str,"%d%d",&n,&m);        int id=1;        mp.clear();        memset(r,1,sizeof r);        for (int i=0;i<=n;++i) v[i].clear();        for (int i=0;i<n;++i)        {            scanf("%s %d",&str,&d);            if (mp.find(str)==mp.end()) mp[str]=id++;//cout<<str<<" "<<mp[str]<<endl;            int u=mp[str];            c[u]=d;            gets(str);            stringstream ss(str);            while (ss>>name)            {                if (mp.find(name)==mp.end())                {                    mp[name]=id++;                }                int to=mp[name];                r[to]=false;                v[u].push_back(to);            }        }        for (int i=1;i<=n;++i)            if (r[i]) v[0].push_back(i);        c[0]=inf;        dfs(0);        int ans=inf;        for (int i=m;i<=n;++i)            ans=min(ans,dp[0][i]);        cout<<ans<<endl;    }}
0 0