POJ3345---bribing FIPA (tree-shaped dp+ backpack)

Source: Internet
Author: User

Description

There is going to being a voting at FIPA (Fédération Internationale de Programmation Association) to determine the host of th E Next IPWC (International Programming World Cup). Benjamin Bennett, the delegation of Diamondland to FIPA, was 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 was no need to diamond-bribe every country, since there was small poor countries that take vot E orders from their respected superpowers. So, if you bribe a country, you had gained the vote of any other country under its domination (both directly and via othe R countries domination). For example, if C are under domination of B, and B is under domination of A, one could get the vote of all three countries Ju St by bribing A. Note that no country are under domination of more than one country, and the domination relationship makes No CYCle. You is to help him, against a big diamond, by writing a program to find out the minimum number of diamonds needed th 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 the integers n (1≤n≤200) and M (0≤m≤n) which is the number of countr IES participating in the voting process, and the number of votes Diamondland needs. The next n lines, each describing one country, is of the following form:

CountryName diamondcount DCName1 DCName1 ...

CountryName, the name of the country, is a string of at least one and at most of the letters and Diamondcount are a positive I Nteger which is the number of diamonds needed to get the vote of this country and all of the countries that their names co Me in the list DCName1 DCName1 which means they is under direct domination of that country. Note that it was possible that some countries does not has any other country under domination. The end of the input is marked by a and 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 Aland
Coland 15
#

Sample Output

20

Source
Tehran 2006

Simple tree-shaped DP backpack
Dp[u][num] means the cost of NUM cities in a subtree rooted in U
Input note, don't get it wrong.

/************************************************************************* > File Name:POJ3345.cpp > Auth Or:alex > Mail: [email protected] > Created time:2015 May 11 Monday 20:24 34 seconds ******************************** ****************************************/#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <bitset>#include <set>#include <vector>using namespace STD;Const DoublePI =ACOs(-1.0);Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-15;typedef Long LongLL;typedefPair <int,int> PLL;intnum[ About];intin_deg[ About];intdp[ About][ About]; vector <int>tree[ About];intw[ About]; Map <string, int>mpCharstr[ the];Charstr2[ the];stringtmpvoidDP (intu) {Num[u] =1;intSize = Tree[u].size (); dp[u][0] =0;if(!size) {dp[u][1] = W[u];return; } for(inti =0; i < size; ++i) {intv = tree[u][i];        DP (v);    Num[u] + = Num[v]; } Dp[u][num[u]] = W[u]; for(inti =0; i < size; ++i) {intv = tree[u][i]; for(intj = Num[u]-1; J >=0; --J) { for(intK =0; K <= J && K <= Num[v];            ++K) {Dp[u][j] = min (Dp[u][j], dp[v][k] + dp[u][j-k]); }        }    }}intMain () {intN, M; while(~scanf('%s ', str)) {if(str[0] ==' # ') { Break;        } mp.clear (); n =0;scanf("%d", &m); for(inti =0; Str[i]! =' + '; ++i) {n = n *Ten+ Str[i]-' 0 '; }intCNT =0;memset(DP, INF,sizeof(DP)); for(inti =0; I <= N;            ++i) {tree[i].clear (); In_deg[i] =0; } getchar (); for(inti =1; I <= N; ++i) {getline (Cin, TMP);intLen = Tmp.length ();intj =0; while(Tmp[j]! ="') {Str[j] = tmp[j];            ++j; } Str[j] =' + ';if(Mp.find (str) = = Mp.end ())            {MP[STR] = ++cnt; } W[mp[str]] =0; ++j; while(J < Len && Tmp[j] >=' 0 '&& Tmp[j] <=' 9 ') {W[mp[str]] = w[mp[str]] *Ten+ tmp[j++]-' 0 '; }if(j = len) {Continue; }intLen2 =0; ++j; for(; j < Len; ++j) {if(Tmp[j] = ="') {Str2[len2] =' + ';if(Mp.find (str2) = = Mp.end ())                    {MP[STR2] = ++cnt;                    } tree[mp[str]].push_back (Mp[str2]);                    ++IN_DEG[MP[STR2]]; Len2 =0; }Else{str2[len2++] = tmp[j]; }} Str2[len2] =' + ';if(Mp.find (str2) = = Mp.end ())            {MP[STR2] = ++cnt;            } tree[mp[str]].push_back (Mp[str2]);        ++IN_DEG[MP[STR2]]; } for(inti =1; I <= N; ++i) {if(!in_deg[i]) {tree[0].push_back (i); w[0] + = W[i]; }} DP (0);intans = inf; for(inti = m; I <= N; ++i) {ans = min (ans, dp[0][i]); }printf("%d\n", ans); }return 0;}

POJ3345---bribing FIPA (tree-shaped dp+ backpack)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.