日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[POJ 3345] Bribing FIPA

發布時間:2025/6/17 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 波岛野结衣 | 在线亚洲一区二区 | 亚洲专区av | 中出一区二区 | 夜夜夜操 | 99热在线观看免费 | 欧美另类在线视频 | 毛片视频网址 | 亚洲综合久久网 | 逼逼av| 国产成人精品一区二区 | 美国色视频 | 涩涩网站在线观看 | wwwjizzzcom| 中国丰满熟妇xxxx性 | 男女啊啊啊 | 日本成人激情 | 日韩夜夜高潮夜夜爽无码 | 色呦呦在线 | 国产激情视频一区 | 影音先锋丝袜制服 | 少妇特黄一区二区三区 | 亚洲熟乱 | 亚洲一区二区三区色 | 免费看黄色三级三级 | 无码人妻丰满熟妇区毛片蜜桃精品 | 久久久中文字幕 | 天堂网在线视频 | 天堂在线播放 | 鲁一鲁av | 波多野结衣一区二区三区在线观看 | 不用播放器av | 日本亲近相奷中文字幕 | 欧美日韩在线视频免费 | 激情综合网五月婷婷 | 青青草社区视频 | www久久 | 精品久久人妻av中文字幕 | 操人视频免费看 | 羞羞草影院 | 韩国av毛片 | 色99在线| 别揉我奶头啊嗯一区二区 | 日本aⅴ视频 | 小黄网站在线观看 | 精品午夜视频 | 成人手机在线视频 | 国产亚洲精 | 国产a网| 国产免费看av | 日韩欧美一区视频 | 精品少妇白浆小泬60P | 午夜激情视频在线观看 | 国产综合福利 | 亚洲美女视频 | 今天高清视频在线观看视频 | 日日操夜夜操天天操 | 久草免费福利视频 | 又黄又色 | 伊人99re| 国产妇女视频 | 操比视频网站 | 黄色一级毛片 | 欧美一区二区三区四区在线 | 久久不卡| 亚洲欧洲日本国产 | 九色视频网站 | 激情五月婷婷综合 | 国产激情在线播放 | 欧美日韩国产电影 | 大桥未久视频在线观看 | 一本到免费视频 | 交专区videossex另类 | 91亚洲精品久久久久久久久久久久 | 国产一区二区成人 | 老牛嫩草二区三区观影体验 | 国产成人一区二区三区视频 | 国产自产 | 成熟了的熟妇毛茸茸 | 三年中国片在线高清观看 | 国产网站免费观看 | 天天爱天天插 | 天天综合天天干 | 在线观看福利网站 | 奇米一区二区三区 | 青青草自拍偷拍 | 亚洲av成人片色在线观看高潮 | 五月婷在线视频 | 亚洲一区二区小说 | 国产亚洲精品成人 | 国产成人精品在线观看 | 18一20岁毛毛片| 97超碰97| 99热99re6国产在线播放 | 日韩色网 | 久久五月天综合 | 亚洲美女中文字幕 | 自拍偷拍第一页 | 韩国精品一区 |