Bribing FIPA - POJ 3345 树形dp+01背包
来源:互联网 发布:最新看电影软件 编辑:程序博客网 时间:2024/05/22 03:34
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#
题意:当你贿赂一个国家后,他的所有子国家也会投票,问至少要m个国家投票的最小花费是多少。
思路:在一棵树上做01背包,注意坑爹的数据输入,只有最后一行才有 #。
AC代码如下:
#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<string>#include<iostream>#include<vector>using namespace std;map<string,int> match;int n,m,dp[210][310],DP[2][310],num[210],cost[210],vis[210];vector<int> vc[210];char c,s[210];string str;int read(){ cin>>str; if(match[str]==0) { num[0]++; match[str]=num[0]; return num[0]; } else return match[str];}bool kong(){ c=cin.peek(); while(c==' ') { cin.ignore(); c=cin.peek(); } if(c=='\n') return false; else return true;}void dfs(int u){ int i,j,k,v,len=vc[u].size(),a,b; num[u]=0; for(i=0;i<len;i++) dfs(vc[u][i]); for(i=1;i<=n;i++) DP[0][i]=1e9; DP[0][0]=0; for(i=0;i<len;i++) { if(i&1) a=1,b=0; else a=0,b=1; v=vc[u][i]; for(j=0;j<=num[u]+num[v];j++) DP[b][j]=1e9; for(j=0;j<=num[v];j++) for(k=0;k<=num[u];k++) DP[b][j+k]=min(DP[b][j+k],DP[a][k]+dp[v][j]); num[u]+=num[v]; } for(i=1;i<=num[u];i++) dp[u][i]=DP[b][i]; num[u]++; dp[u][num[u]]=cost[u];}int main(){ int i,j,k,u,v,t=0,ans; while(~scanf("%d%d",&n,&m)) { t++; for(i=0;i<=n;i++) vc[i].clear(); num[0]=0; match.clear(); for(i=1;i<=n;i++) { u=read(); scanf("%d",&k); cost[u]=k; while(kong()) { v=read(); vc[u].push_back(v); vis[v]=t; } } for(i=1;i<=n;i++) if(vis[i]!=t) vc[0].push_back(i); dfs(0); ans=dp[0][n]; for(j=m;j<=n;j++) ans=min(ans,dp[0][j]); printf("%d\n",ans); c=cin.peek(); while(c==' ' || c=='\n') { cin.ignore(); c=cin.peek(); } if(c=='#') break; } scanf("%s",s);}
- poj 3345 Bribing FIPA[树形dp+背包]
- Bribing FIPA - POJ 3345 树形dp+01背包
- poj 3345 Bribing FIPA 【树形dp + 01背包】
- poj 3345 Bribing FIPA (树形背包dp | 输入坑)
- (中等) 树形dp(分组背包) POJ 3345 Bribing FIPA
- POJ 3345 Bribing FIPA(树形dp+背包)
- poj 3345 Bribing FIPA 树形背包
- POJ 3345 Bribing FIPA 树形DP
- POJ 3345 Bribing FIPA 树形dp
- poj 3345 Bribing FIPA 树形dp
- 【树形DP】 POJ 3345 Bribing FIPA
- POJ 3345 Bribing FIPA / 树形DP
- POJ 3345 Bribing FIPA 树形dp
- poj 3345 Bribing FIPA(树形dp)
- poj 3345 Bribing FIPA 树形dp
- POJ-3345 Bribing FIPA(Tree dp + 背包)
- POJ3345---Bribing FIPA(树形dp+背包)
- POJ 3345 Bribing FIPA 树形背包+字符串处理
- 解决Android与服务器交互大容量数据问题
- UVA - 573 The Snail
- android音乐播放器开发 SweetMusicPlayer 智能加载在线歌词
- HttpClient简单爬虫
- 开始准备找工作
- Bribing FIPA - POJ 3345 树形dp+01背包
- 成为优秀程序员的最佳学习方式
- 安卓导入项目报错Unable to resolve target 'android-xx'问题解决
- leetcode - Unique Binary Search Trees
- UML类图几种关系总结
- Codeforces 263 D. Cycle in Graph
- 计算机应用技术(计算机与信息技术相关学科)
- 凭谎硬旁碳厝家靡酒啦幕孜俦杜丶
- 计算机应用(重点学科)