[POJ 3345] Bribing FIPA
問題描述
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.
輸入格式
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.
輸出格式
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.
樣例輸入
3 2
Aland 10
Boland 20 Aland
Coland 15
樣例輸出
20
題目大意
有一棵n個節點的樹,給一個節點染色的話,該節點的子樹的所有節點都會被染色。而給每一個節點染色都有一個代價,要求用最少的代價使被染色的節點數量不少于m。
解析
最直接的想法是設\(f[i][k]\)表示有i個節點被染色時的最小代價。同時設\(size[i]\)表示以\(i\)為根的子樹大小,那么容易得到以下狀態轉移方程:
\[ f[i][k]=min(f[i-size[j]][k]+w[i])_{j\in i} \]
但在實現時還有些細節需要注意。為了節約空間,可以在每一層遞歸中開一個數組記錄當前的\(f\)的值,這樣就可以節約掉\(f\)的第二維。
這道題的毒瘤輸入也值得一提......
代碼
#include <iostream> #include <cstdio> #include <cstring> #include <map> #define N 402 using namespace std; int head[N],ver[N*2],nxt[N*2],l; int f[N],w[N],din[N],size[N],n,m,i,cnt,ans=0; char s[102]; string u,v; map<string,int> d; void insert(int x,int y) {l++;din[y]++;ver[l]=y;nxt[l]=head[x];head[x]=l; } void init(int x,int pre) {size[x]=1;for(int i=head[x];i;i=nxt[i]){int y=ver[i];if(y!=pre){init(y,x);size[x]+=size[y];}} } void dp(int x,int y) {int tmp[N]={0};for(int i=0;i<=n;i++) tmp[i]=f[i];for(int i=head[x];i;i=nxt[i]){int y=ver[i];dp(y,x);}for(int i=n;i>=size[x];i--){tmp[i]=min(tmp[i],tmp[i-size[x]]+w[x]);f[i]=min(f[i],tmp[i]);} } int main() {while(gets(s)){if(s[0]=='#') break;sscanf(s,"%d%d",&n,&m);memset(f,0x3f,sizeof(f));memset(head,0,sizeof(head));memset(din,0,sizeof(din));d.clear();f[0]=l=cnt=0;for(i=1;i<=n;i++){int p;cin>>u>>p;if(d[u]==0) d[u]=++cnt;w[d[u]]=p;char c=getchar();while(c==' '){cin>>v;if(d[v]==0) d[v]=++cnt;insert(d[u],d[v]);c=getchar();}}for(i=1;i<=n;i++){if(din[i]==0) init(i,0);}for(i=1;i<=n;i++){if(din[i]==0) dp(i,0);}ans=1<<30;for(i=m;i<=n;i++) ans=min(ans,f[i]);cout<<ans<<endl;}return 0; }轉載于:https://www.cnblogs.com/LSlzf/p/10973537.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的[POJ 3345] Bribing FIPA的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaScript的学习
- 下一篇: Sratch-gui 中文文档