DP Dynamic Programming study notes - the advanced version of the

To say how it can Gugu Gu liver?

DP do not understand or want to ask the foundation to start learning from the venue on a blog DP: DP Dynamic Programming Study Notes

This is a blog we will be divided into two parts (so you do not Gugu Gu of ...) , the article is more difficult to some DP, next is a means to optimize various DP.

- positive start - (why I recently wrote a blog like this)

Backpack tree DP , DP tree in a very Kichiku subject.

Simply say is this: a packet backpack tree. I do not know grouped backpack also go on a study.

Let's look at a board question: elective

Then we analyze the problem (is best to first think about it), since each course Prerequisite is only one, so it is easy to think of this relationship by storing a tree structure -> only from the Father child node to node. However, the data will form the topic of a forest. Think about it, Why?

Yes, it may be more than one course no Prerequisite. But we only learned the tree DP , how to do it? Simple, we own the whole chant out of a tree.

But the original parent-child relationship can not change, how the whole of it? We create a new "virtual classes" 0 come out as "No Prerequisite of course" Prerequisite. That we put every tree woods are connected to a new --0 root node, so that we get a tree.

The idea is not it wonderful? This question then is converted into a backpack grouped in a tree run.

Then we come to set the state. will not? Then you do not learn . Previous blog read yet? Fast to see.

We talked about the previous, tree DP generally each node x as the first dimension ...

and then? (Hammer), the packet backpack ah. Oh...

So we get the state: Let f [x, j] indicates the highest credit we get from x to select sub-tree rooted at j course can.

Very simple is not it? State equation are out do not know baa? Then we work together to push a push bar.

We define a few variables ease of description, let son (x) represents a collection of child nodes x, siz (x) denotes the number of child nodes of x, x if this elective course, then we would for any y∈son (x) can be selected from the sub-tree rooted at y in several courses (denoted ci) to repair ... meet Σci = t-1 basis, we try to repair the highest credits. We have siz (x) group of articles, each article has j-1, of which the volume of the k-th item of the i-th group of articles k, value of f [y, k], the total volume of the backpack is j-1 .

We elect up an item from each group, i.e., up to a transition to the state of each child node x. After completing our x, j-1 may also choose courses, so we want to "bulk goods" no more than the case of the j-1, the credit (value) maximum. That we "lesson in how much the y" as the article.

Then we tree DP, get the answer:

 

void dp(int x){
    f[x][0]=0;
    for(int i=0;i<son[x].size();i++){
        int y=son[x][i];
        dp(y);
        for ( int J = m + . 1 ; J> = . 1 ; T--) // virtual node mandatory, it is. 1 + m 
            for ( int K = 0 ; K <= J- . 1 ; ++ K) within the group // article, the volume of the first group of the k-th item x (k selected courses) for k 
                F [x] [J] = max (F [x] [J], F [x] [JK] + F [Y] [ K]);
    }
}

 

The main function section, we read every f [i] [1] as the value of the i-th course -> explanation: from i to select a sub-tree of a class -> value is f [i] [1 ].

Then we continue to discuss the tree DP.

We are currently doing the tree DP DP issues are carried out in a tree root tree, if the subject is given an unrooted trees?

Do we need to point to the root of each tree DP have to do a statistical answer? Of course not, and if it do not like violence.

For this general problem, we use a secondary scan (change root method) to resolve.

This is also known as DP change root DP , what? You want the title?

No , (explosive hammer dog's head) ... take Accumulation Degree .

This seems to be my first time to questions on the POJ, Pio hook I did not question how the brush-off, but the quality looks very good.

But I always like Los Valley .jpg , the EMM, you say you do not understand? Chrome comes with translation , not the skin without skin.

Well.

给你一个树形的水系(废话那么多懒得翻译了)

有一个有n个节点,n-1条河道的树形水系,每个河道有一个最大容水量c[x][y]c[x][y]表示点x到y的最大容水量,源点可以源源不断出水,以源点作为根节点的树的叶子结点可以无限接纳水,而一个节点水的流量等于流过其儿子节点的水的流量之和,儿子节点水的流量不能超过其与父亲连边的最大容水量,询问最大的源点水流量,n2×10^5。

其实简单点说就是求树形结构上的最大流,但是我们不知道源点。为什么不用网络流?嘘,数据太狗了。

于是我们来快乐地DP啊。不给我源点?我每个点都DP一遍

要是这样能过我还写它干嘛。

不过还是先讲讲怎么DP吧。假设我们知道根节点是x。

那么我们来拆问题了。你看这个问题它又大又烦,不如把它拆掉。

考虑用树形DP拆问题。对于每个节点x,我们发现它只能流向自己的子树,于是可以这样设状态,我们用f[x]表示在以x为根的子树中,以x为源点流向子树的最大流量。然后我们对于每个子树都可以拆成小子树,再拆,再拆...就到叶节点了,问题就得解了。

切,刚刚还那么傲娇的大问题现在还不是被脱的一件不剩,看到DP的魅力了吧?再大的问题,只要你是DP题,我就能把你拆掉。

然后我们就很自然的得到了转移方程式:

$$f[x]=\sum\limits_{\text{y∈son(x)}} \begin{cases} min(f[x],c(x,y))& \text{x=0}\\ c(x,y)& \text{x!=0} \end{cases}$$

Latex新手写上面那个公式写了半个小时...

待填坑

Guess you like

Origin www.cnblogs.com/light-house/p/11827497.html