Poj-3345 hdu-2415
Question
There are n countries. You need to get support from M countries. To get support from country I, you need to pay the price for cost [I ].
Some of these countries have relationships with elders and younger siblings. That is to say, if you have received support from a certain country,
All the younger siblings in this country (including younger siblings... recursive) will support you free of charge.
The minimum cost is supported by M countries.
Ideas
This is a pretty nice tree-like DP, but the input is a little troublesome. At first, I thought that the end of each group of samples was '#', and the result was always
Re, later found that the last group is '#'...
Because the country name is directly given, I use Map <string, int> to map and save the number.
The relationship between elders and younger siblings is actually a tree composed of trees. The relationship between all countries is a forest.
To facilitate tree-like DP, a "Super root node" is added. The root node of all trees in the forest is the son of a "Super root node.
Then, f (I, j) is used to represent subtree I, and the minimum cost for obtaining support from J countries is
For subtree I, the son nodes of all node I are a group of items,
For a son, you can choose to enable him to support 1, 2... and J. Then, all the sons are grouped into backpacks ..
TOT [v] indicates the number of nodes of subtree v.
Status transfer:
F [u] [I] = min {f [u] [I], f [u] [I-j] + F [v] [J] | 1 <= j <= tot [v] & J <= I & V is u son };
Code