首页 > 代码库 > POJ3345 Bribing FIPA
POJ3345 Bribing FIPA
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 5021 | Accepted: 1574 |
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 2Aland 10Boland 20 AlandColand 15#
Sample Output
20
Source
标准树形DP。
但是读入数据神烦,要是没有stl的map,更烦。
1 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 #include<map> 8 #include<vector> 9 using namespace std;10 const int INF=0x3f3f3f3f;11 const int mxn=300;12 char s[mxn];13 int cnt=0;14 map<string,int>mp;15 vector<int>e[mxn];16 bool fa[mxn];17 int num[mxn];18 int w[mxn];//价值 19 int n,m;20 int f[mxn][mxn];//[根结点][选用子结点数量]=最优解 21 void init(){22 int i,j;23 for(i=0;i<=n;i++)e[i].clear();24 mp.clear();25 memset(fa,0,sizeof fa);26 memset(f,0x3f,sizeof f);27 memset(num,0,sizeof num);28 cnt=0;29 return;30 }31 32 void dp(int rt){33 // for(int i=1;i<=n;i++) f[rt][i]=INF;34 f[rt][0]=0;35 int i,j,k;36 num[rt]=1;37 for(i=0;i<e[rt].size();i++){38 int v=e[rt][i];//紫树39 dp(v);40 num[rt]+=num[v];41 for(j=n;j>=0;--j){//选用子结点数量 42 for(k=0;k<=j;++k){43 f[rt][j]=min(f[rt][j],f[rt][j-k]+f[v][k]);44 }45 }46 }47 f[rt][num[rt]]=min(f[rt][num[rt]],w[rt]);48 return;49 }50 int main(){51 char str[mxn];52 while(fgets(str,200,stdin)){53 if(str[0]==‘#‘)break;54 sscanf(str,"%d%d",&n,&m);55 init();56 int i,j;57 for(i=1;i<=n;i++){58 scanf("%s",s);59 if(!mp.count(s)){60 mp[s]=++cnt;61 }62 scanf("%d",&w[mp[s]]);63 while(getchar()!=‘\n‘){64 scanf("%s",str);65 if(!mp.count(str)){mp[str]=++cnt;}66 e[mp[s]].push_back(mp[str]);67 fa[mp[str]]=1;68 }69 }70 for(i=1;i<=n;i++){71 if(!fa[i]) e[0].push_back(i);72 }73 w[0]=INF;74 dp(0);75 int ans=INF;76 for(i=m;i<=n;i++){77 ans=min(ans,f[0][i]);78 }79 printf("%d\n",ans);80 }81 return 0;82 }
POJ3345 Bribing FIPA