Poj 3345 bribing FIPA (tree backpack DP | input pitfall)

Source: Internet
Author: User



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

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.