加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

poj3345 Bribing FIPA【树形DP】【背包】

发布时间:2020-12-14 04:19:09 所属栏目:大数据 来源:网络整理
导读:Bribing FIPA Time Limit: ?2000MS ? Memory Limit: ?65536K Total Submissions: ?5910 ? Accepted: ?1850 Description There is going to be a voting at FIPA (Fédération Internationale de Programmation Association) to determine the host of the n
Bribing FIPA
Time Limit:?2000MS ? Memory Limit:?65536K
Total Submissions:?5910 ? Accepted:?1850

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

Source

Tehran 2006

?

题意:

给定一棵树,节点之间有隶属关系,每个节点有一个贿赂值。当你贿赂了一个节点之后,他的所有子孙也都被你贿赂了就不需要贿赂了。现在想找m个国家给你投票,只有贿赂了他们才会给你投票,希望花费的贿赂值是最小的。

思路:

这道题疯狂RE,读入太毒了,后来发现其实是map和vector可能没清干净。要注意!

一个树上的背包。不太会写。

总的只有n个节点,就是体积。价值就是贿赂值,但是希望是尽量小。

  1 //#include <bits/stdc++.h>
  2 #include<iostream>
  3 #include<cmath>
  4 #include<algorithm>
  5 #include<stdio.h>
  6 #include<cstring>
  7 #include<vector>
  8 #include<map>
  9 #include<set>
 10 
 11 #define inf 0x3f3f3f3f
 12 using namespace std;
 13 typedef long long LL;
 14 
 15 int n,m,cnt = 0;
 16 const int maxn = 205;
 17 map<string,int>mp;
 18 struct node{
 19     //int id;
 20     int diamond;
 21     vector<int>son;
 22 }country[maxn];
 23 bool vis[maxn];
 24 int dp[maxn],temp[maxn];
 25 
 26 int dfsdp(int rt,int ID)
 27 {
 28     int last = 0,now = 0;//last和now相当于dfs序,用来表示rt子树的一个区间
 29     dp[ID] = 0;
 30     for(int i = 0; i < country[rt].son.size(); i++){
 31         int child = country[rt].son[i];
 32         now = dfsdp(child,ID + last + 1) + 1;//id表示这是当前第几个城市
 33         for(int j = last + 1; j <= last + now; j++){
 34             dp[ID + j] = inf;
 35         }
 36         for(int j = last; j >= 0; j--){
 37             for(int k = 1; k < now; k++){
 38                 dp[ID + j + k] = min(dp[ID + j + k],dp[ID + j] + temp[k]);
 39             }
 40             dp[ID + j + now] = min(dp[ID + j + now],dp[ID + j] + country[child].diamond);
 41         }
 42         last += now;
 43     }
 44     
 45     for(int i = 1; i <= last; i++){
 46         temp[i] = dp[ID + i];
 47     }
 48     return last;
 49 }
 50 
 51 int read()
 52 {
 53     char c = getchar();
 54     int s = 0;
 55     while(c < 0 || c > 9){
 56         if(c == #)return 0;
 57         c = getchar();
 58     }
 59     while(c >= 0 &&c <= 9){
 60         s *= 10;
 61         s += c - 0;
 62         c = getchar();
 63     }
 64     return s;
 65 }
 66 
 67 int main(){
 68     //char ch;
 69     char str[100];
 70     bool flag = true;
 71     while(gets(str)){
 72         if(str[0] == #)break;
 73         sscanf(str,"%d%d",&n,&m);
 74         cnt = 0;
 75         memset(vis,false,sizeof(vis));
 76         mp.clear();
 77         for(int i = 0; i <= n; i++){
 78             country[i].son.clear();
 79         }
 80         for(int i = 0; i < n; i++){
 81             scanf("%s",str);
 82             int d;
 83             if(mp.find(str) == mp.end()){
 84                 mp[str] = ++cnt;
 85             }
 86             scanf("%d",&d);
 87             int now = mp[str];
 88             country[mp[str]].diamond = d;
 89             //getchar();
 90             while(getchar() != n){
 91                 scanf("%s",str);
 92                 if(mp.find(str) == mp.end()){
 93                     mp[str] = ++cnt;
 94                 }
 95                 //cout<<mp[name]<<endl;
 96                 country[now].son.push_back(mp[str]);
 97                 vis[mp[str]] = true;
 98             }
 99         }
100         for(int i = 1; i <= cnt; i++){
101             if(!vis[i]){
102                 country[0].son.push_back(i);
103             }
104         }
105         country[0].diamond = inf;
106         dfsdp(0,0);
107         int ans = inf;
108         for(int i = m; i <= n; i++){
109             ans = min(ans,dp[i]);
110         }
111         printf("%dn",ans);
112 
113     }
114 
115     return 0;
116 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读